From 5b21b1db58414d5fbbde00ae0cc03e4dc2d00f80 Mon Sep 17 00:00:00 2001 From: Reimar Date: Thu, 5 Dec 2024 18:03:34 +0100 Subject: [PATCH] Solve day 3 part 1 --- day_3.sml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 day_3.sml diff --git a/day_3.sml b/day_3.sml new file mode 100644 index 0000000..4d89f1c --- /dev/null +++ b/day_3.sml @@ -0,0 +1,30 @@ +fun numberAt (i : int) = fn (s : string) => + if i >= String.size s then NONE + else SOME (Substring.takel Char.isDigit (Substring.extract (s, i, NONE))) + +fun substrToInt (n : substring) = valOf (Int.fromString (Substring.string n)) + +fun sum (l : int list) : int = foldl (op +) 0 l + +val file = TextIO.openIn "day_3.txt" +val input = TextIO.inputAll file; +TextIO.closeIn file; + +val instructions = List.tabulate ((size input), (fn i => + let + val n1 = numberAt (i + 4) input + val n2 = Option.mapPartial (fn n => numberAt (i + 5 + Substring.size n) input) n1 + in + if isSome n1 andalso isSome n2 andalso + String.substring (input, i, 4) = "mul(" andalso + String.sub (input, i + 4 + Substring.size (valOf n1)) = #"," andalso + String.sub (input, i + 5 + Substring.size (valOf n1) + Substring.size (valOf n2)) = #")" + then SOME (n1, n2) else NONE +end)) + +val instructions = List.mapPartial + (fn (num) => case num of SOME (SOME x, SOME y) => SOME (substrToInt x, substrToInt y) | _ => NONE) + instructions + +val result = sum (map (op *) instructions) +