Compare commits
No commits in common. "37a4944b0896e47b3d9b39b40d87b070cc32d2cf" and "b17f36064caedec86c18798b969e4fec6622f8ed" have entirely different histories.
37a4944b08
...
b17f36064c
14
day_1.sml
14
day_1.sml
@ -1,13 +1,13 @@
|
|||||||
fun sortAsc (l: int list) : int list = ListMergeSort.sort (op >) l
|
val file = TextIO.openIn "day_1.txt";
|
||||||
|
|
||||||
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
41
day_2.sml
@ -1,41 +0,0 @@
|
|||||||
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