Solve day one using ML
This commit is contained in:
parent
68d5191021
commit
e9c8abef6a
20
day_1.sml
Normal file
20
day_1.sml
Normal 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;
|
||||
|
Loading…
Reference in New Issue
Block a user