From b8f1b26106c1c59a86e521edd7b9296ee57cbb48 Mon Sep 17 00:00:00 2001 From: DJ O'Leary Date: Mon, 8 Dec 2025 02:33:54 +0100 Subject: feat(2025d4): solve part 2 solution: 8254 --- 2025/ocaml/lib/application/d04p02.ml | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/2025/ocaml/lib/application/d04p02.ml b/2025/ocaml/lib/application/d04p02.ml index c53811a..75b1236 100644 --- a/2025/ocaml/lib/application/d04p02.ml +++ b/2025/ocaml/lib/application/d04p02.ml @@ -3,10 +3,18 @@ open Domain let err_empty_input = "input is empty" let rec count_accessible_rolls matrix = - let count_paper lst = List.length @@ List.filter (fun c -> c <> ".") lst in - let accessible_rolls_of_paper = ref 0 in - count_accessible_rolls_aux matrix count_paper accessible_rolls_of_paper ; - !accessible_rolls_of_paper + let count_paper lst = List.length @@ List.filter (fun c -> c = "@") lst in + let accessible_rolls_of_paper = ref [] in + let no_change = ref false in + while not !no_change do + let accessible_rolls_of_paper' = !accessible_rolls_of_paper in + count_accessible_rolls_aux matrix count_paper accessible_rolls_of_paper ; + if + List.length accessible_rolls_of_paper' + = List.length !accessible_rolls_of_paper + then no_change := true + done ; + List.length !accessible_rolls_of_paper and count_accessible_rolls_aux matrix count_paper accessible_rolls_of_paper = for y = 0 to Array.length matrix - 1 do @@ -15,16 +23,19 @@ and count_accessible_rolls_aux matrix count_paper accessible_rolls_of_paper = else let neighbour_coords = Matrix.M.get_valid_neighbours matrix x y in let neighbours = - List.map (fun (x', y') -> Matrix.M.get matrix x' y') neighbour_coords + List.map (fun (nx, ny) -> Matrix.M.get matrix nx ny) neighbour_coords in let paper_count = count_paper neighbours in let is_accessible = paper_count < 4 in if is_accessible then - accessible_rolls_of_paper := !accessible_rolls_of_paper + 1 ; + accessible_rolls_of_paper := (x, y) :: !accessible_rolls_of_paper ; print_iteration_debug_msg x y neighbour_coords neighbours paper_count is_accessible done - done + done ; + List.iter + (fun (x', y') -> Matrix.M.set matrix x' y' ".") + !accessible_rolls_of_paper and print_iteration_debug_msg x y neighbour_coords neighbours paper_count is_accessible = -- cgit v1.2.3