From fa3e6d8d880e680405c477d58ba61f3bd8e9d52d Mon Sep 17 00:00:00 2001 From: Acvaxoort Date: Thu, 12 Dec 2024 18:06:53 +0100 Subject: [PATCH] Changed corner detection code so it looks more functional-programming-y --- 12/main.ml | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/12/main.ml b/12/main.ml index 1db63d3..eaba72c 100644 --- a/12/main.ml +++ b/12/main.ml @@ -29,15 +29,17 @@ let count_corners_no_rotation bitmask = let corner_bitmask = Int.logand bitmask 0x07 in if corner_bitmask = 0x00 || corner_bitmask = 0x5 || corner_bitmask = 0x2 then 1 else 0 +let rec multiple_application_list f arg n = + if n <= 0 then + [arg] + else + arg :: multiple_application_list f (f arg) (n - 1) + let count_corners bitmask = - let rot1 = rotate_8bit_left_twice bitmask in - let rot2 = rotate_8bit_left_twice rot1 in - let rot3 = rotate_8bit_left_twice rot2 in - count_corners_no_rotation bitmask - + count_corners_no_rotation rot1 - + count_corners_no_rotation rot2 - + count_corners_no_rotation rot3 - + multiple_application_list rotate_8bit_left_twice bitmask 3 + |> List.map count_corners_no_rotation + |> List.fold_left Int.add 0 + let is_same_value value arr (x, y) = try arr.(y).(x) = value @@ -85,7 +87,7 @@ let find_all_regions_cost arr = |> List.map (fun (area, sides, _) -> area * sides) |> List.fold_left Int.add 0 in let price2 = !regions - |> List.map (fun (area, _, corners) -> (*printf "%d %d\n" area corners;*) area * corners) + |> List.map (fun (area, _, corners) -> area * corners) |> List.fold_left Int.add 0 in (price1, price2)