fun deleteNth (l : int list, n : int) = List.take (l, n) @ List.drop (l, n + 1) fun isDecreasing (l : int list) = ListPair.all (fn (a, b) => b < a andalso b >= a - 3) (l, tl l) fun isIncreasing (l : int list) = ListPair.all (fn (a, b) => b > a andalso b <= a + 3) (l, tl l) fun withTolerance (f : (int list) -> bool) = fn (l : int list) => let val withRemoved = List.tabulate (List.length l, fn (i) => deleteNth (l, i)) in List.exists f withRemoved end val file = TextIO.openIn "day_2.txt" val input = TextIO.inputAll file; TextIO.closeIn file; val lines = String.tokens (fn (char) => char = #"\n") input val reports = map (fn (line) => let val numbers = String.tokens Char.isSpace line in map (fn (n) => valOf (Int.fromString n)) numbers end ) lines (* Part 1 *) val safeReports = List.filter (fn (report) => isDecreasing report orelse isIncreasing report) reports val safeAmount = List.length safeReports (* Part 2 *) val toleratedReports = List.filter (fn (report) => withTolerance isDecreasing report orelse withTolerance isIncreasing report) reports val toleratedAmount = List.length toleratedReports