Solve day 1 part 2
This commit is contained in:
parent
ceac8f51f2
commit
b17f36064c
13
day_1.sml
13
day_1.sml
@ -25,7 +25,20 @@ val lists = (
|
||||
sortAsc (map (fn (pair) => List.last pair) pairs)
|
||||
)
|
||||
|
||||
(* Part 1 *)
|
||||
|
||||
val distances = ListPair.map (fn (n1, n2) => abs(n1 - n2)) lists
|
||||
|
||||
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
|
||||
|
||||
val similarityScore = sum similarities
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user