adventofcode-2023/day_1.sml

21 lines
534 B
Standard ML
Raw Normal View History

2023-12-06 22:41:03 +00:00
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;