Skip to content

Commit

Permalink
More optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
bertptrs committed Dec 22, 2024
1 parent 321d78d commit 329cd4e
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions 2024/src/aoc/days/day22.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy
from numpy.lib.stride_tricks import sliding_window_view

from . import SeparateRunner
from . import CombinedRunner


def advance(secrets: numpy.array) -> numpy.array:
Expand All @@ -15,18 +15,9 @@ def advance(secrets: numpy.array) -> numpy.array:
return new_secrets


class DayRunner(SeparateRunner):
class DayRunner(CombinedRunner):
@classmethod
def part1(cls, input: str) -> int:
secrets = numpy.fromstring(input, dtype=int, sep="\n")

for _ in range(2000):
secrets = advance(secrets)

return secrets.sum()

@classmethod
def part2(cls, input: str) -> int:
def run_both(cls, input: str) -> tuple[int, int]:
secrets = numpy.fromstring(input, dtype=int, sep="\n")

progression = [secrets]
Expand All @@ -37,15 +28,14 @@ def part2(cls, input: str) -> int:

field = numpy.stack(progression, axis=-1) % 10
delta = field[:, 1:] - field[:, :-1]
windows = sliding_window_view(delta, 4, axis=1)

per_signal = defaultdict(int)

for row_scores, row_deltas in zip(field, delta):
unique, positions = numpy.unique(
sliding_window_view(row_deltas, 4), return_index=True, axis=0
)
for row_scores, row_deltas in zip(field, windows):
unique, positions = numpy.unique(row_deltas, return_index=True, axis=0)

for key, index in zip(unique, positions):
per_signal[tuple(key)] += row_scores[index + 4]
for key, bananas in zip(unique, row_scores[positions + 4]):
per_signal[tuple(key)] += bananas

return max(per_signal.values())
return secrets.sum(), max(per_signal.values())

0 comments on commit 329cd4e

Please sign in to comment.