Solve day 1 part 2

This commit is contained in:
Reimar 2024-12-03 21:56:17 +01:00
parent ceac8f51f2
commit b17f36064c
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268

View File

@ -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