Use same printer for both parts

We want to change Aoc.main to take a single printer parameter to
simplify the run process.
This commit is contained in:
2024-12-24 20:28:52 +00:00
parent 030fd73bab
commit 43b47b2a34
2 changed files with 10 additions and 14 deletions

View File

@@ -129,7 +129,8 @@ let rec execute_until_halted vm =
| false -> execute_until_halted (execute_insn vm) | false -> execute_until_halted (execute_insn vm)
(** [string_of_ouput vm] gives the output of [vm]. *) (** [string_of_ouput vm] gives the output of [vm]. *)
let string_of_output vm = let part1 vm =
let vm = execute_until_halted vm in
List.rev vm.out |> List.map string_of_int |> String.concat "," List.rev vm.out |> List.map string_of_int |> String.concat ","
(** [scan_digit acc ip vm] updates the acc for A so that the output of running (** [scan_digit acc ip vm] updates the acc for A so that the output of running
@@ -159,8 +160,8 @@ let scan_all vm =
impl 0 (Array.length vm.code - 1) impl 0 (Array.length vm.code - 1)
(** [string_of_a vm] returns the A register of [vm]. *) (** [string_of_a vm] returns the A register of [vm]. *)
let string_of_a vm = string_of_int vm.a let part2 vm =
let vm = scan_all vm in
string_of_int vm.a
let _ = let _ = Aoc.main vm_of_file [ (Fun.id, part1); (Fun.id, part2) ]
Aoc.main vm_of_file
[ (string_of_output, execute_until_halted); (string_of_a, scan_all) ]

View File

@@ -104,12 +104,12 @@ let find_route_length count grid =
let part1 count rocks = let part1 count rocks =
match find_route_length count rocks with match find_route_length count rocks with
| None -> failwith "part1" | None -> failwith "part1"
| Some (cost, _) -> cost | Some (cost, _) -> string_of_int cost
(** [part2 start_count grid] returns the location of the first rock to fall into (** [part2 start_count grid] returns the location of the first rock to fall into
[grid] which makes it impossible to get from the top-left to bottom-right. [grid] which makes it impossible to get from the top-left to bottom-right.
*) *)
let part2 start_count grid = let part2 width start_count grid =
(* Implementation notes: (* Implementation notes:
We do this by binary search in impl. The left_count is a known count of We do this by binary search in impl. The left_count is a known count of
@@ -137,16 +137,11 @@ let part2 start_count grid =
let count = impl start_count (1 + count_rocks 0 0) in let count = impl start_count (1 + count_rocks 0 0) in
match Array.find_index (( = ) (count - 1)) grid.grid with match Array.find_index (( = ) (count - 1)) grid.grid with
| None -> failwith "part2" | None -> failwith "part2"
| Some idx -> idx | Some idx -> Printf.sprintf "%d,%d" (idx mod width) (idx / width)
(** [string_of_idx width idx] prints the (x, y) location for a given index in a
grid. *)
let string_of_idx width idx =
Printf.sprintf "%d,%d" (idx mod width) (idx / width)
(** Width of grid *) (** Width of grid *)
let width = 71 let width = 71
let _ = let _ =
Aoc.main (grid_of_file width) Aoc.main (grid_of_file width)
[ (string_of_int, part1 1024); (string_of_idx width, part2 1024) ] [ (Fun.id, part1 1024); (Fun.id, part2 width 1024) ]