From 321d78d850980b235e3c5eeb18bb78083b07d3f0 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Sun, 22 Dec 2024 12:15:11 +0100 Subject: [PATCH] More loops to numpy --- 2024/src/aoc/days/day22.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/2024/src/aoc/days/day22.py b/2024/src/aoc/days/day22.py index 71ca375..a0dfcb8 100644 --- a/2024/src/aoc/days/day22.py +++ b/2024/src/aoc/days/day22.py @@ -41,14 +41,11 @@ def part2(cls, input: str) -> int: per_signal = defaultdict(int) for row_scores, row_deltas in zip(field, delta): - seen = set() - - for window, price in zip( - sliding_window_view(row_deltas, 4), row_scores[4:] - ): - key = tuple(window) - if key not in seen: - seen.add(key) - per_signal[key] += price + unique, positions = numpy.unique( + sliding_window_view(row_deltas, 4), return_index=True, axis=0 + ) + + for key, index in zip(unique, positions): + per_signal[tuple(key)] += row_scores[index + 4] return max(per_signal.values())