Solve day 2 part 1

This commit is contained in:
Reimar 2024-12-04 17:04:52 +01:00
parent 137c751f44
commit 67aea117c3
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268

23
day_2.sml Normal file
View File

@ -0,0 +1,23 @@
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