were multi-file now baybee
This commit is contained in:
parent
2eae917f47
commit
de277999f4
31
bin/day1.ml
Normal file
31
bin/day1.ml
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
let part1 lines =
|
||||
let rec most_calories most current lines =
|
||||
match lines with
|
||||
| [] -> most
|
||||
| "" :: lines -> most_calories (max most current) 0 lines
|
||||
| line :: lines ->
|
||||
let calories = int_of_string line in
|
||||
most_calories most (current + calories) lines
|
||||
in
|
||||
most_calories 0 0 lines
|
||||
;;
|
||||
|
||||
let part2 lines =
|
||||
let accum_calories elves calories =
|
||||
match elves, calories with
|
||||
| elves, "" -> 0 :: elves
|
||||
| elf :: elves, calories -> (elf + int_of_string calories) :: elves
|
||||
| elves, _ -> elves
|
||||
in
|
||||
let calories_per_elf = List.fold_left accum_calories [ 0 ] lines in
|
||||
match List.sort (fun x y -> y - x) calories_per_elf with
|
||||
| x1 :: x2 :: x3 :: _ -> x1 + x2 + x3
|
||||
| _ -> failwith "wtf"
|
||||
;;
|
||||
|
||||
let print =
|
||||
let lines = Utils.lines_of_file "inputs/day1.tt" in
|
||||
Printf.(
|
||||
printf "Day 1.1: %d\n" @@ part1 lines;
|
||||
printf "Day 1.2: %d\n" @@ part2 lines)
|
||||
;;
|
||||
94
bin/day2.ml
Normal file
94
bin/day2.ml
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
type move =
|
||||
| Rock
|
||||
| Paper
|
||||
| Scissors
|
||||
|
||||
type outcome =
|
||||
| Win
|
||||
| Lose
|
||||
| Draw
|
||||
|
||||
let parse_move = function
|
||||
| "A" | "X" -> Rock
|
||||
| "B" | "Y" -> Paper
|
||||
| "C" | "Z" -> Scissors
|
||||
| char -> failwith @@ Printf.sprintf "Char %s does not correspond to a move" char
|
||||
;;
|
||||
|
||||
let parse_outcome = function
|
||||
| "X" -> Lose
|
||||
| "Y" -> Draw
|
||||
| "Z" -> Win
|
||||
| char -> failwith @@ Printf.sprintf "Char %s does not correspond to an outcome" char
|
||||
;;
|
||||
|
||||
let participation_score = function
|
||||
| Rock -> 1
|
||||
| Paper -> 2
|
||||
| Scissors -> 3
|
||||
;;
|
||||
|
||||
let match_score = function
|
||||
| Win -> 6
|
||||
| Draw -> 3
|
||||
| Lose -> 0
|
||||
;;
|
||||
|
||||
let beats = function
|
||||
| Rock -> Paper
|
||||
| Paper -> Scissors
|
||||
| Scissors -> Rock
|
||||
;;
|
||||
|
||||
let loses_to = function
|
||||
| Rock -> Scissors
|
||||
| Paper -> Rock
|
||||
| Scissors -> Paper
|
||||
;;
|
||||
|
||||
let match_outcome = function
|
||||
| them, me when me = beats them -> Win
|
||||
| them, me when me = them -> Draw
|
||||
| _, _ -> Lose
|
||||
;;
|
||||
|
||||
let part1 lines =
|
||||
let parse_line line =
|
||||
match Re.Pcre.(split ~rex:(regexp {|\s+|}) line) with
|
||||
| them :: me :: _ -> parse_move them, parse_move me
|
||||
| _ -> failwith @@ Printf.sprintf "line `%s` contains more than two moves" line
|
||||
in
|
||||
List.fold_left
|
||||
(fun total_score line ->
|
||||
let them, me = parse_line line in
|
||||
total_score + participation_score me + match_score (match_outcome (them, me)))
|
||||
0
|
||||
lines
|
||||
;;
|
||||
|
||||
let part2 lines =
|
||||
let parse_line =
|
||||
function%pcre
|
||||
| {|(?<them>\w)\s*(?<outcome>\w)|} -> parse_move them, parse_outcome outcome
|
||||
| line -> failwith @@ Printf.sprintf "line `%s` doesn't match re" line
|
||||
in
|
||||
List.fold_left
|
||||
(fun total_score line ->
|
||||
let them, outcome = parse_line line in
|
||||
let me =
|
||||
match outcome with
|
||||
| Win -> beats them
|
||||
| Draw -> them
|
||||
| Lose -> loses_to them
|
||||
in
|
||||
total_score + participation_score me + match_score outcome)
|
||||
0
|
||||
lines
|
||||
;;
|
||||
|
||||
let print =
|
||||
let lines = Utils.lines_of_file "./inputs/day2.tt" in
|
||||
Printf.(
|
||||
printf "Day 2.1: %d\n" @@ part1 lines;
|
||||
printf "Day 2.2: %d\n" @@ part2 lines)
|
||||
;;
|
||||
131
bin/main.ml
131
bin/main.ml
|
|
@ -1,131 +1,4 @@
|
|||
let lines_of_file filename = Stdio.In_channel.(with_file filename ~f:input_lines)
|
||||
|
||||
let rec print_int_list = function
|
||||
| [] -> ()
|
||||
| e :: l ->
|
||||
print_int e;
|
||||
print_string " ";
|
||||
print_int_list l
|
||||
;;
|
||||
|
||||
let day1_part1 lines =
|
||||
let rec most_calories most current lines =
|
||||
match lines with
|
||||
| [] -> most
|
||||
| "" :: lines -> most_calories (max most current) 0 lines
|
||||
| line :: lines ->
|
||||
let calories = int_of_string line in
|
||||
most_calories most (current + calories) lines
|
||||
in
|
||||
most_calories 0 0 lines
|
||||
;;
|
||||
|
||||
let day1_part2 lines =
|
||||
let accum_calories elves calories =
|
||||
match elves, calories with
|
||||
| elves, "" -> 0 :: elves
|
||||
| elf :: elves, calories -> (elf + int_of_string calories) :: elves
|
||||
| elves, _ -> elves
|
||||
in
|
||||
let calories_per_elf = List.fold_left accum_calories [ 0 ] lines in
|
||||
match List.sort (fun x y -> y - x) calories_per_elf with
|
||||
| x1 :: x2 :: x3 :: _ -> x1 + x2 + x3
|
||||
| _ -> failwith "wtf"
|
||||
;;
|
||||
|
||||
type move =
|
||||
| Rock
|
||||
| Paper
|
||||
| Scissors
|
||||
|
||||
type outcome =
|
||||
| Win
|
||||
| Lose
|
||||
| Draw
|
||||
|
||||
let parse_move = function
|
||||
| "A" | "X" -> Rock
|
||||
| "B" | "Y" -> Paper
|
||||
| "C" | "Z" -> Scissors
|
||||
| char -> failwith @@ Printf.sprintf "Char %s does not correspond to a move" char
|
||||
;;
|
||||
|
||||
let parse_outcome = function
|
||||
| "X" -> Lose
|
||||
| "Y" -> Draw
|
||||
| "Z" -> Win
|
||||
| char -> failwith @@ Printf.sprintf "Char %s does not correspond to an outcome" char
|
||||
;;
|
||||
|
||||
let participation_score = function
|
||||
| Rock -> 1
|
||||
| Paper -> 2
|
||||
| Scissors -> 3
|
||||
;;
|
||||
|
||||
let match_score = function
|
||||
| Win -> 6
|
||||
| Draw -> 3
|
||||
| Lose -> 0
|
||||
;;
|
||||
|
||||
let beats = function
|
||||
| Rock -> Paper
|
||||
| Paper -> Scissors
|
||||
| Scissors -> Rock
|
||||
;;
|
||||
|
||||
let loses_to = function
|
||||
| Rock -> Scissors
|
||||
| Paper -> Rock
|
||||
| Scissors -> Paper
|
||||
;;
|
||||
|
||||
let match_outcome = function
|
||||
| them, me when me = beats them -> Win
|
||||
| them, me when me = them -> Draw
|
||||
| _, _ -> Lose
|
||||
;;
|
||||
|
||||
let day2_part1 lines =
|
||||
let parse_line line =
|
||||
match Re.Pcre.(split ~rex:(regexp {|\s+|}) line) with
|
||||
| them :: me :: _ -> parse_move them, parse_move me
|
||||
| _ -> failwith @@ Printf.sprintf "line `%s` contains more than two moves" line
|
||||
in
|
||||
List.fold_left
|
||||
(fun total_score line ->
|
||||
let them, me = parse_line line in
|
||||
total_score + participation_score me + match_score (match_outcome (them, me)))
|
||||
0
|
||||
lines
|
||||
;;
|
||||
|
||||
let day2_part2 lines =
|
||||
let parse_line =
|
||||
function%pcre
|
||||
| {|(?<them>\w)\s*(?<outcome>\w)|} -> parse_move them, parse_outcome outcome
|
||||
| line -> failwith @@ Printf.sprintf "line `%s` doesn't match re" line
|
||||
in
|
||||
List.fold_left
|
||||
(fun total_score line ->
|
||||
let them, outcome = parse_line line in
|
||||
let me =
|
||||
match outcome with
|
||||
| Win -> beats them
|
||||
| Draw -> them
|
||||
| Lose -> loses_to them
|
||||
in
|
||||
total_score + participation_score me + match_score outcome)
|
||||
0
|
||||
lines
|
||||
;;
|
||||
|
||||
let () =
|
||||
(* print_int @@ day1_part1 @@ lines_of_file "input1.txt"; *)
|
||||
(* print_int @@ day1_part2 @@ lines_of_file "input1.txt"; *)
|
||||
print_int @@ day2_part1 @@ lines_of_file "input2.txt";
|
||||
print_newline ();
|
||||
print_int @@ day2_part2 @@ lines_of_file "input2.txt";
|
||||
print_newline ()
|
||||
Day1.print;
|
||||
Day2.print
|
||||
;;
|
||||
|
|
|
|||
9
bin/utils.ml
Normal file
9
bin/utils.ml
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
let lines_of_file filename = Stdio.In_channel.(with_file filename ~f:input_lines)
|
||||
|
||||
let rec print_int_list = function
|
||||
| [] -> ()
|
||||
| e :: l ->
|
||||
print_int e;
|
||||
print_string " ";
|
||||
print_int_list l
|
||||
;;
|
||||
Loading…
Reference in New Issue
Block a user