Solve day 2 part 2
This commit is contained in:
parent
67aea117c3
commit
37a4944b08
22
day_2.sml
22
day_2.sml
@ -1,9 +1,15 @@
|
||||
fun isDecreasing (l: int list) : bool =
|
||||
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) : bool =
|
||||
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;
|
||||
@ -17,7 +23,19 @@ val reports = map
|
||||
)
|
||||
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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user