Day 2024 day 11 part 1 and incredibly slow part 2

This commit is contained in:
2024-12-11 08:07:26 +00:00
parent b2e56f802e
commit b9e3907e4d
2 changed files with 51 additions and 2 deletions

47
bin/day2411.ml Normal file
View File

@@ -0,0 +1,47 @@
let load_file fname =
match In_channel.with_open_text fname In_channel.input_line with
| Some x -> x
| None -> failwith "load_file"
let log10i i =
let rec impl acc = function 0 -> acc | x -> impl (acc + 1) (x / 10) in
assert (i > 0);
impl ~-1 i
let digits10 i = 1 + log10i i
(** [pow10 n] returns [10] raised to the [n]th power. [n] must be non-negative.
*)
let pow10 n =
let rec impl acc = function 0 -> acc | x -> impl (acc * 10) (x - 1) in
assert (n >= 0);
impl 1 n
let rec apply_n n fn arg = if n <= 0 then arg else apply_n (n - 1) fn (fn arg)
(*
let print_int_list lst =
List.iter
(fun i ->
print_int i;
print_char ' ')
lst;
print_newline ();
()
*)
let calc n input =
let rec step_rec acc = function
| [] -> acc
| 0 :: t -> step_rec (1 :: acc) t
| x :: t when digits10 x mod 2 = 0 ->
let pow = pow10 (digits10 x / 2) in
let left = x / pow in
let right = x mod pow in
step_rec (right :: left :: acc) t
| x :: t -> step_rec ((x * 2024) :: acc) t
in
apply_n n (step_rec []) input
let part1 str = Aoc.ints_of_string str |> calc 25 |> List.length
let part2 str = Aoc.ints_of_string str |> calc 75 |> List.length
let _ = Aoc.main load_file [ (string_of_int, part1); (string_of_int, part2) ]

View File

@@ -9,7 +9,8 @@
day2407
day2408
day2409
day2410)
day2410
day2411)
(names
day2401
day2402
@@ -20,5 +21,6 @@
day2407
day2408
day2409
day2410)
day2410
day2411)
(libraries str aoc))