Skip to content

Commit

Permalink
day 11 memo ftw
Browse files Browse the repository at this point in the history
  • Loading branch information
viandoxdev committed Dec 11, 2024
1 parent 363ddac commit 85e43ab
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
45 changes: 44 additions & 1 deletion 2024/days/Day11.ml
Original file line number Diff line number Diff line change
@@ -1 +1,44 @@
let day11 _input = ("TODO", "TODO")
open Utils

let parse =
let open Angstrom in
Result.get_ok
% parse_string ~consume:All (sep_by (char ' ') digits <* char '\n')

let step m =
let open Iter in
let memo = Hashtbl.create 2048 in

let rec count depth stone =
if depth >= m then 1
else
let count_next = count (depth + 1) in

let cached = Hashtbl.find_opt memo (stone, depth) in
match cached with
| Some res -> res
| None ->
let res =
match stone with
| 0 -> count_next 1
| s ->
let digits = log10 s in
if digits mod 2 = 0 then
let p = pows.(digits / 2) in
let left = s / p in
let right = s - (left * p) in

count_next left + count_next right
else count_next (s * 2024)
in
Hashtbl.add memo (stone, depth) res;
res
in
fun stones -> of_list stones |> map (count 0) |> sum

let part1 = step 25
let part2 = step 75

let day11 input =
let stones = parse input in
(string_of_int @@ part1 stones, string_of_int @@ part2 stones)
1 change: 1 addition & 0 deletions 2024/lib/Aoc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ let days =
(8, Day08.day08);
(9, Day09.day09);
(10, Day10.day10);
(11, Day11.day11);
]

let run_day ctx (day, fn) =
Expand Down

0 comments on commit 85e43ab

Please sign in to comment.