Skip to content

Commit

Permalink
day22 part b python
Browse files Browse the repository at this point in the history
  • Loading branch information
KatieLG committed Dec 22, 2024
1 parent fe558f1 commit 6b9cfc9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
4 changes: 2 additions & 2 deletions AOC/data/day_22/sample_part_two.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
1
10
100
2
3
2024
33 changes: 31 additions & 2 deletions AOC/python/solutions/day_22.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

from models.aoc_solution import AOCSolution

type Seq = tuple[int, int, int, int]


class Day22(AOCSolution):
EXPECTED = {
"part_one": {"sample": 37327623, "data": 14726157693},
"part_two": {"sample": 0, "data": 0},
"part_two": {"sample": 23, "data": 1614},
}

def __post_init__(self) -> None:
Expand All @@ -23,14 +25,41 @@ def process(self, secret: int):
secret = self.mix_and_prune(secret, secret * 2048)
return secret

def generate_prices(self, secret: int, count: int) -> list[int]:
prices = [secret % 10]
for _ in range(count):
secret = self.process(secret)
prices.append(secret % 10)
return prices

def difference_values(self, secret: int, count: int) -> dict[Seq, int]:
"""Generate all sequences of 4 integers"""
prices = self.generate_prices(secret, count)
differences = [prices[i + 1] - prices[i] for i in range(count - 1)]
values: dict[Seq, int] = {}
for i in range(count - 4):
tup = (
differences[i],
differences[i + 1],
differences[i + 2],
differences[i + 3],
)
if tup not in values:
values[tup] = prices[i + 4]
return values

def part_one(self) -> int:
secrets = self.secrets
for _ in range(2000):
secrets = [self.process(secret) for secret in secrets]
return sum(secrets)

def part_two(self) -> int:
return 0
"""2000 changes = 2001 secret values"""
dicts = [self.difference_values(secret, 2001) for secret in self.secrets]
unique_seqs = {key for seq in dicts for key in seq}
sales = {seq: sum(d[seq] for d in dicts if seq in d) for seq in unique_seqs}
return max(sales.values())


if __name__ == "__main__":
Expand Down

0 comments on commit 6b9cfc9

Please sign in to comment.