Format
This commit is contained in:
parent
67fd4e2085
commit
88321a2614
34
day_1.sml
34
day_1.sml
@ -1,6 +1,6 @@
|
||||
fun sortAsc (l: int list) : int list = ListMergeSort.sort (op >) l
|
||||
fun sortAsc (l : int list) : int list = ListMergeSort.sort (op >) l
|
||||
|
||||
fun sum (l: int list) : int = foldl (op +) 0 l
|
||||
fun sum (l : int list) : int = foldl (op +) 0 l
|
||||
|
||||
fun substrToInt (substr: substring) : int = valOf (Int.fromString (Substring.string substr))
|
||||
|
||||
@ -11,18 +11,18 @@ TextIO.closeIn file;
|
||||
val lines = String.tokens (fn (char) => char = #"\n") input
|
||||
|
||||
val pairs = map
|
||||
(fn (line) =>
|
||||
let val substr = Substring.extract (line, 0, NONE)
|
||||
in [
|
||||
substrToInt (Substring.takel Char.isDigit substr),
|
||||
substrToInt (Substring.taker Char.isDigit substr)
|
||||
] end
|
||||
)
|
||||
lines
|
||||
(fn (line) =>
|
||||
let val substr = Substring.extract (line, 0, NONE)
|
||||
in [
|
||||
substrToInt (Substring.takel Char.isDigit substr),
|
||||
substrToInt (Substring.taker Char.isDigit substr)
|
||||
] end
|
||||
)
|
||||
lines
|
||||
|
||||
val lists = (
|
||||
sortAsc (map (fn (pair) => hd pair) pairs),
|
||||
sortAsc (map (fn (pair) => List.last pair) pairs)
|
||||
sortAsc (map (fn (pair) => hd pair) pairs),
|
||||
sortAsc (map (fn (pair) => List.last pair) pairs)
|
||||
)
|
||||
|
||||
(* Part 1 *)
|
||||
@ -34,11 +34,11 @@ val totalDistance = sum distances
|
||||
(* Part 2 *)
|
||||
|
||||
val similarities = ListPair.map
|
||||
(fn (n1, n2) =>
|
||||
let val frequency = List.length (List.filter (fn (n) => n = n1) (#2 lists))
|
||||
in n1 * frequency end
|
||||
)
|
||||
lists
|
||||
(fn (n1, n2) =>
|
||||
let val frequency = List.length (List.filter (fn (n) => n = n1) (#2 lists))
|
||||
in n1 * frequency end
|
||||
)
|
||||
lists
|
||||
|
||||
val similarityScore = sum similarities
|
||||
|
||||
|
22
day_2.sml
22
day_2.sml
@ -1,14 +1,14 @@
|
||||
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)
|
||||
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)
|
||||
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
|
||||
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;
|
||||
@ -17,11 +17,11 @@ 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
|
||||
(fn (line) =>
|
||||
let val numbers = String.tokens Char.isSpace line
|
||||
in map (fn (n) => valOf (Int.fromString n)) numbers end
|
||||
)
|
||||
lines
|
||||
|
||||
(* Part 1 *)
|
||||
|
||||
@ -32,8 +32,8 @@ val safeAmount = List.length safeReports
|
||||
(* Part 2 *)
|
||||
|
||||
val toleratedReports = List.filter
|
||||
(fn (report) => withTolerance isDecreasing report orelse withTolerance isIncreasing report)
|
||||
reports
|
||||
(fn (report) => withTolerance isDecreasing report orelse withTolerance isIncreasing report)
|
||||
reports
|
||||
|
||||
val toleratedAmount = List.length toleratedReports
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user