Solve day 1 part 1
This commit is contained in:
commit
ceac8f51f2
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
day_*.txt
|
||||
|
31
day_1.sml
Normal file
31
day_1.sml
Normal file
@ -0,0 +1,31 @@
|
||||
val file = TextIO.openIn "day_1.txt";
|
||||
val input = TextIO.inputAll file;
|
||||
TextIO.closeIn file;
|
||||
|
||||
fun sortAsc (l: int list) : int list = ListMergeSort.sort (fn (a, b) => a > b) l
|
||||
|
||||
fun sum (l: int list) : int = foldl (fn (a, b) => a + b) 0 l
|
||||
|
||||
fun substrToInt(substr: substring) : int = valOf (Int.fromString (Substring.string substr))
|
||||
|
||||
val lines = String.tokens (fn (char) => char = #"\n") input
|
||||
|
||||
val pairs = map
|
||||
(fn (line) =>
|
||||
let val substr = Substring.extract (line, 0, NONE)
|
||||
in [
|
||||
substrToInt (Substring.takel Char.isDigit substr),
|
||||
substrToInt (Substring.taker Char.isDigit substr)
|
||||
] end
|
||||
)
|
||||
lines
|
||||
|
||||
val lists = (
|
||||
sortAsc (map (fn (pair) => hd pair) pairs),
|
||||
sortAsc (map (fn (pair) => List.last pair) pairs)
|
||||
)
|
||||
|
||||
val distances = ListPair.map (fn (n1, n2) => abs(n1 - n2)) lists
|
||||
|
||||
val totalDistance = sum distances
|
||||
|
Loading…
Reference in New Issue
Block a user