Skip to content

Commit 321d78d

Browse files
committed
More loops to numpy
1 parent 230b4ae commit 321d78d

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

2024/src/aoc/days/day22.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,11 @@ def part2(cls, input: str) -> int:
4141
per_signal = defaultdict(int)
4242

4343
for row_scores, row_deltas in zip(field, delta):
44-
seen = set()
45-
46-
for window, price in zip(
47-
sliding_window_view(row_deltas, 4), row_scores[4:]
48-
):
49-
key = tuple(window)
50-
if key not in seen:
51-
seen.add(key)
52-
per_signal[key] += price
44+
unique, positions = numpy.unique(
45+
sliding_window_view(row_deltas, 4), return_index=True, axis=0
46+
)
47+
48+
for key, index in zip(unique, positions):
49+
per_signal[tuple(key)] += row_scores[index + 4]
5350

5451
return max(per_signal.values())

0 commit comments

Comments
 (0)