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