rename bin to src and delete boring dirs

This commit is contained in:
ryan committed 2023-09-21 19:06:02 -07:00
1 parent de277999f4
commit dd0acc4ddb
9 files changed
+1 -5

No files matched your search

+31
View 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
View 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)
;;
+5
View File
@@ -0,0 +1,5 @@
(executable
(public_name aoc)
(name main)
(libraries stdio re)
(preprocess (pps ppx_regexp)))
+4
View File
@@ -0,0 +1,4 @@
let () =
Day1.print;
Day2.print
;;
+9
View 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
;;