Changed corner detection code so it looks more functional-programming-y

This commit is contained in:
2024-12-12 18:06:53 +01:00
parent 219e28bc55
commit fa3e6d8d88

View File

@@ -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)