This commit is contained in:
ryan committed 2023-09-21 18:17:39 -07:00
commit 2eae917f47
13 files changed
+4955

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
(executable
(public_name aoc)
(name main)
(libraries aoc stdio re)
(preprocess (pps ppx_regexp)))
+2253
View File
File diff suppressed because it is too large. Load diff
+2500
View File
File diff suppressed because it is too large. Load diff
+131
View File
@@ -0,0 +1,131 @@
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 ()
;;