2024 Day 3 part 1

This commit is contained in:
2024-12-03 08:08:45 +00:00
parent f0648b6267
commit d57ffe17cb
2 changed files with 25 additions and 2 deletions

23
bin/day2403.ml Normal file
View File

@@ -0,0 +1,23 @@
let matched_group_opt n s = try Some (Str.matched_group n s) with _ -> None
let find_nums s =
let r = Str.regexp {|mul(\([0-9][0-9]?[0-9]?\),\([0-9][0-9]?[0-9]?\))|} in
let rec impl acc pos =
try
let _ = Str.search_forward r s pos in
let p0 = Str.matched_group 0 s in
let p1 = matched_group_opt 1 s in
let p2 = matched_group_opt 2 s in
impl
((p0, Option.map int_of_string p1, Option.map int_of_string p2) :: acc)
(Str.match_end ())
with Not_found -> acc
in
impl [] 0
let nums_from_file fname =
Aoc.strings_from_file fname |> List.map find_nums |> List.concat
let mac acc a b = acc + (Option.value a ~default:0 * Option.value b ~default:0)
let day2403a = List.fold_left (fun acc (_, a, b) -> mac acc a b) 0
let _ = Aoc.main nums_from_file [ (string_of_int, day2403a) ]

View File

@@ -1,4 +1,4 @@
(executables
(public_names day2401 day2402)
(names day2401 day2402)
(public_names day2401 day2402 day2403)
(names day2401 day2402 day2403)
(libraries str aoc))