2024 day 4 part 2.

Not happy with this part - not so functional.
This commit is contained in:
2024-12-04 15:30:05 +00:00
parent 374a16befe
commit 003fac75d6

View File

@@ -43,5 +43,28 @@ let find_xmas sa =
List.fold_left (fun acc x -> acc + search_string x "XMAS") 0 search_strings List.fold_left (fun acc x -> acc + search_string x "XMAS") 0 search_strings
+ List.fold_left (fun acc x -> acc + search_string x "SAMX") 0 search_strings + List.fold_left (fun acc x -> acc + search_string x "SAMX") 0 search_strings
let find_mas sa x y =
let find_ms a b c d =
sa.(y - 1).[x - 1] = a
&& sa.(y - 1).[x + 1] = b
&& sa.(y + 1).[x - 1] = c
&& sa.(y + 1).[x + 1] = d
in
sa.(y).[x] = 'A'
&& (find_ms 'M' 'M' 'S' 'S' || find_ms 'S' 'S' 'M' 'M'
|| find_ms 'M' 'S' 'M' 'S' || find_ms 'S' 'M' 'S' 'M')
let find_mases sa =
let acc = ref 0 in
for y = 1 to Array.length sa - 2 do
for x = 1 to String.length sa.(y) - 2 do
if find_mas sa x y then acc := !acc + 1
done
done;
!acc
let sa_of_file fname = Aoc.strings_of_file fname |> Array.of_list let sa_of_file fname = Aoc.strings_of_file fname |> Array.of_list
let _ = Aoc.main sa_of_file [ (string_of_int, find_xmas) ]
let _ =
Aoc.main sa_of_file
[ (string_of_int, find_xmas); (string_of_int, find_mases) ]