Solve day 2 part 1
This commit is contained in:
parent
137c751f44
commit
67aea117c3
23
day_2.sml
Normal file
23
day_2.sml
Normal 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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user