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))
|
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 lines = String.tokens (fn (char) => char = #"\n") input
|
||||||
|
|
||||||
val pairs = map
|
val pairs = map
|
||||||
(fn (line) =>
|
(fn (line) =>
|
||||||
let val substr = Substring.extract (line, 0, NONE)
|
let val substr = Substring.extract (line, 0, NONE)
|
||||||
in [
|
in [
|
||||||
substrToInt (Substring.takel Char.isDigit substr),
|
substrToInt (Substring.takel Char.isDigit substr),
|
||||||
substrToInt (Substring.taker Char.isDigit substr)
|
substrToInt (Substring.taker Char.isDigit substr)
|
||||||
] end
|
] end
|
||||||
)
|
)
|
||||||
lines
|
lines
|
||||||
|
|
||||||
val lists = (
|
val lists = (
|
||||||
sortAsc (map (fn (pair) => hd pair) pairs),
|
sortAsc (map (fn (pair) => hd pair) pairs),
|
||||||
sortAsc (map (fn (pair) => List.last pair) pairs)
|
sortAsc (map (fn (pair) => List.last pair) pairs)
|
||||||
)
|
)
|
||||||
|
|
||||||
(* Part 1 *)
|
(* Part 1 *)
|
||||||
@ -34,11 +34,11 @@ val totalDistance = sum distances
|
|||||||
(* Part 2 *)
|
(* Part 2 *)
|
||||||
|
|
||||||
val similarities = ListPair.map
|
val similarities = ListPair.map
|
||||||
(fn (n1, n2) =>
|
(fn (n1, n2) =>
|
||||||
let val frequency = List.length (List.filter (fn (n) => n = n1) (#2 lists))
|
let val frequency = List.length (List.filter (fn (n) => n = n1) (#2 lists))
|
||||||
in n1 * frequency end
|
in n1 * frequency end
|
||||||
)
|
)
|
||||||
lists
|
lists
|
||||||
|
|
||||||
val similarityScore = sum similarities
|
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 deleteNth (l : int list, n : int) = List.take (l, n) @ List.drop (l, n + 1)
|
||||||
|
|
||||||
fun isDecreasing (l : int list) =
|
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) =
|
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) =>
|
fun withTolerance (f : (int list) -> bool) = fn (l : int list) =>
|
||||||
let val withRemoved = List.tabulate (List.length l, fn (i) => deleteNth (l, i))
|
let val withRemoved = List.tabulate (List.length l, fn (i) => deleteNth (l, i))
|
||||||
in List.exists f withRemoved end
|
in List.exists f withRemoved end
|
||||||
|
|
||||||
val file = TextIO.openIn "day_2.txt"
|
val file = TextIO.openIn "day_2.txt"
|
||||||
val input = TextIO.inputAll file;
|
val input = TextIO.inputAll file;
|
||||||
@ -17,11 +17,11 @@ TextIO.closeIn file;
|
|||||||
val lines = String.tokens (fn (char) => char = #"\n") input
|
val lines = String.tokens (fn (char) => char = #"\n") input
|
||||||
|
|
||||||
val reports = map
|
val reports = map
|
||||||
(fn (line) =>
|
(fn (line) =>
|
||||||
let val numbers = String.tokens Char.isSpace line
|
let val numbers = String.tokens Char.isSpace line
|
||||||
in map (fn (n) => valOf (Int.fromString n)) numbers end
|
in map (fn (n) => valOf (Int.fromString n)) numbers end
|
||||||
)
|
)
|
||||||
lines
|
lines
|
||||||
|
|
||||||
(* Part 1 *)
|
(* Part 1 *)
|
||||||
|
|
||||||
@ -32,8 +32,8 @@ val safeAmount = List.length safeReports
|
|||||||
(* Part 2 *)
|
(* Part 2 *)
|
||||||
|
|
||||||
val toleratedReports = List.filter
|
val toleratedReports = List.filter
|
||||||
(fn (report) => withTolerance isDecreasing report orelse withTolerance isIncreasing report)
|
(fn (report) => withTolerance isDecreasing report orelse withTolerance isIncreasing report)
|
||||||
reports
|
reports
|
||||||
|
|
||||||
val toleratedAmount = List.length toleratedReports
|
val toleratedAmount = List.length toleratedReports
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user