adventofcode-2024/day_2.sml
2024-12-04 17:04:52 +01:00

24 lines
690 B
Standard ML

fun isDecreasing (l: int list) : bool =
ListPair.all (fn (a, b) => b < a andalso b >= a - 3) (l, tl l)
fun isIncreasing (l: int list) : bool =
ListPair.all (fn (a, b) => b > a andalso b <= a + 3) (l, tl l)
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
val safeReports = List.filter (fn (report) => isDecreasing report orelse isIncreasing report) reports
val safeAmount = List.length safeReports