Compare commits

...

3 Commits

Author SHA1 Message Date
37a4944b08
Solve day 2 part 2 2024-12-04 17:56:12 +01:00
67aea117c3
Solve day 2 part 1 2024-12-04 17:04:52 +01:00
137c751f44
Clean up day 1 2024-12-04 16:55:29 +01:00
2 changed files with 48 additions and 7 deletions

View File

@ -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;
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 pairs = map

41
day_2.sml Normal file
View 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