From 443ff2cee67a187244e7eee1804e8b31e13da179 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Sun, 1 Dec 2024 18:04:13 +0100 Subject: [PATCH] Reduce part 2 to O(n) --- 2024/bonus/day01/main.tf | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/2024/bonus/day01/main.tf b/2024/bonus/day01/main.tf index 41fd6b1..9eebb15 100644 --- a/2024/bonus/day01/main.tf +++ b/2024/bonus/day01/main.tf @@ -14,7 +14,9 @@ locals { diffs = [for i in range(length(local.left_sorted)): abs(local.left_sorted[i] - local.right_sorted[i])] - matching = [for left in local.left: sum([for right in local.right: left == right ? left : 0])] + counts = {for num in local.right: num => num...} + + matching = [for left in local.left: left * length(lookup(local.counts, left, []))] } output "part1" {