diff options
| author | DJ O'Leary <dijitol@proton.me> | 2025-12-08 02:07:32 +0100 |
|---|---|---|
| committer | DJ O'Leary <dijitol@proton.me> | 2025-12-08 02:07:32 +0100 |
| commit | 2c82e474679976f23e50baf445de24ac21ac300d (patch) | |
| tree | a7afee638ac3ef65460f67bc412b63dede527c1d /2025 | |
| parent | 0f34d9a931791406d2cabf282742530195f71927 (diff) | |
refactor(2025d4): refactor part 2 to prep for recursion
Diffstat (limited to '2025')
| -rw-r--r-- | 2025/ocaml/lib/application/d04p02.ml | 55 |
1 files changed, 30 insertions, 25 deletions
diff --git a/2025/ocaml/lib/application/d04p02.ml b/2025/ocaml/lib/application/d04p02.ml index 7c69de4..c53811a 100644 --- a/2025/ocaml/lib/application/d04p02.ml +++ b/2025/ocaml/lib/application/d04p02.ml @@ -2,31 +2,29 @@ open Domain let err_empty_input = "input is empty" -let rec solve input () = - if input = "" then Error err_empty_input - else - let matrix = Matrix.M.of_string input in - let count_paper lst = List.length @@ List.filter (fun c -> c <> ".") lst in - let accessible_rolls_of_paper = ref 0 in - for y = 0 to Array.length matrix - 1 do - for x = 0 to Array.length matrix.(y) - 1 do - if matrix.(y).(x) = "." then () - 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 - 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 ; - print_iteration_debug_msg x y neighbour_coords neighbours paper_count - is_accessible - done - done ; - Ok !accessible_rolls_of_paper +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 + +and count_accessible_rolls_aux matrix count_paper accessible_rolls_of_paper = + for y = 0 to Array.length matrix - 1 do + for x = 0 to Array.length matrix.(y) - 1 do + if matrix.(y).(x) = "." then () + 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 + 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 ; + print_iteration_debug_msg x y neighbour_coords neighbours paper_count + is_accessible + done + done and print_iteration_debug_msg x y neighbour_coords neighbours paper_count is_accessible = @@ -46,3 +44,10 @@ and print_iteration_debug_msg x y neighbour_coords neighbours paper_count if acc = "" then " " ^ neighbour ^ "; " else acc ^ neighbour ^ "; " ) "" neighbours ) paper_count is_accessible ) + +let solve input () = + if input = "" then Error err_empty_input + else + let matrix = Matrix.M.of_string input in + let accessible_rolls_of_paper = count_accessible_rolls matrix in + Ok accessible_rolls_of_paper |
