summaryrefslogtreecommitdiff
path: root/ocaml/lib/year2024/day03/part01.ml
diff options
context:
space:
mode:
authorDJ O'Leary <dijitol@proton.me>2026-06-27 04:28:48 +0200
committerDJ O'Leary <dijitol@proton.me>2026-06-27 04:28:48 +0200
commitbc8b9edc0860f4804dfd0ea868ee7beaa564e62a (patch)
treee1123d11627b4829586da3b097015b1dbb3d0285 /ocaml/lib/year2024/day03/part01.ml
parent976048a85403fbb0751c34fa42703a3c47f6e918 (diff)
test: fix ocaml tests 2024 {WIP}HEADmain
Diffstat (limited to 'ocaml/lib/year2024/day03/part01.ml')
-rw-r--r--ocaml/lib/year2024/day03/part01.ml33
1 files changed, 33 insertions, 0 deletions
diff --git a/ocaml/lib/year2024/day03/part01.ml b/ocaml/lib/year2024/day03/part01.ml
new file mode 100644
index 0000000..c12b68a
--- /dev/null
+++ b/ocaml/lib/year2024/day03/part01.ml
@@ -0,0 +1,33 @@
+(* Day 3: Part 1 *)
+
+let find_mul text =
+ let regex_or_err = Re2.create "mul\\(([0-9]{1,3}),([0-9]{1,3})\\)" in
+ match regex_or_err with
+ | Ok regex -> (
+ let matches = Re2.get_matches regex text in
+ match matches with Ok matches -> matches | Error _ -> [])
+ | Error _ -> []
+
+let extract_pairs (matches : Re2.Match.t list) : (int * int) list =
+ List.map
+ (fun x ->
+ let left = Re2.Match.get x ~sub:(`Index 1) in
+ match left with
+ | None -> (0, 0)
+ | Some left -> (
+ let left_num = int_of_string left in
+ let right = Re2.Match.get x ~sub:(`Index 2) in
+ match right with
+ | None -> (0, 0)
+ | Some right ->
+ let right_num = int_of_string right in
+ (left_num, right_num)))
+ matches
+
+let solve input =
+ input |> find_mul |> extract_pairs
+ |> List.fold_left
+ (fun acc pair ->
+ let left, right = pair in
+ acc + (left * right))
+ 0