Compare commits
3 Commits
b17f36064c
...
37a4944b08
Author | SHA1 | Date | |
---|---|---|---|
37a4944b08 | |||
67aea117c3 | |||
137c751f44 |
14
day_1.sml
14
day_1.sml
@ -1,13 +1,13 @@
|
|||||||
val file = TextIO.openIn "day_1.txt";
|
fun sortAsc (l: int list) : int list = ListMergeSort.sort (op >) l
|
||||||
|
|
||||||
|
fun sum (l: int list) : int = foldl (op +) 0 l
|
||||||
|
|
||||||
|
fun substrToInt (substr: substring) : int = valOf (Int.fromString (Substring.string substr))
|
||||||
|
|
||||||
|
val file = TextIO.openIn "day_1.txt"
|
||||||
val input = TextIO.inputAll file;
|
val input = TextIO.inputAll file;
|
||||||
TextIO.closeIn file;
|
TextIO.closeIn file;
|
||||||
|
|
||||||
fun sortAsc (l: int list) : int list = ListMergeSort.sort (fn (a, b) => a > b) l
|
|
||||||
|
|
||||||
fun sum (l: int list) : int = foldl (fn (a, b) => a + b) 0 l
|
|
||||||
|
|
||||||
fun substrToInt(substr: substring) : int = valOf (Int.fromString (Substring.string substr))
|
|
||||||
|
|
||||||
val lines = String.tokens (fn (char) => char = #"\n") input
|
val lines = String.tokens (fn (char) => char = #"\n") input
|
||||||
|
|
||||||
val pairs = map
|
val pairs = map
|
||||||
|
41
day_2.sml
Normal file
41
day_2.sml
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user