Solve day one using ML

This commit is contained in:
Reimar 2023-12-06 23:41:03 +01:00
parent 68d5191021
commit e9c8abef6a
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268

20
day_1.sml Normal file
View File

@ -0,0 +1,20 @@
val file = TextIO.openIn "day_1.txt";
val input = TextIO.inputAll file;
TextIO.closeIn file;
val result =
let val lines = String.tokens (fn (char) => char = #"\n") input
in
let val numbers = map
(fn (line) =>
let val digits = List.filter Char.isDigit (String.explode line)
in Int.fromString (String.implode [List.hd digits, List.last digits]) end)
lines
in
let val numbers = map Option.valOf (List.filter Option.isSome numbers)
in
List.foldl (fn (v, acc) => v + acc) 0 numbers
end
end
end;