Solve 2024 day 22

This commit is contained in:
felixm 2024-12-22 00:31:27 -05:00
parent 3979a60fa4
commit 265829715a
2 changed files with 62 additions and 2 deletions

56
2024/d22.py Normal file
View File

@ -0,0 +1,56 @@
from lib import get_data
from lib import ints
from collections import defaultdict
data = get_data(__file__)
def secret(x):
x ^= x * 64
x %= 16777216
x ^= x // 32
x %= 16777216
x ^= x * 2048
x %= 16777216
return x
def slice(xs, n):
return [slice for slice in zip(*[xs[i:] for i in range(n)])]
codes = defaultdict(int)
def int_seq(x):
xs = []
for _ in range(2000):
xs.append(x % 10)
x = secret(x)
ds = [a - b for a, b in zip(xs[1:], xs)]
seen = set()
for s in slice(list(zip(xs[1:], ds)), 4):
code = tuple([e[1] for e in s])
v = s[-1][0]
if not code in seen:
codes[code] += v
seen.add(code)
for line in data.splitlines():
(x,) = ints(line)
s = 0
for line in data.splitlines():
(x,) = ints(line)
int_seq(x)
for _ in range(2000):
x = secret(x)
s += x
print(s)
print(max(codes.values()))

View File

@ -310,5 +310,9 @@ and focus. Of course, learning more algorithms and techniques helps.
- Day 17: `17:34:16 23722 0 >24h 17778 0`
- Day 18: `14:35:01 20398 0 14:37:18 19550 0`
- Day 19: `00:14:37 2001 0 00:19:43 1584 0`
- Day 20: `01:08:53 3637 0 01:53:01 2837 0`
- Day 20: `01:08:53 3637 0 01:53:01 2837 0`
- Day 21: `00:48:40 215 0 - - -`
- Day 22: `00:13:04 1930 0 00:28:29 739 0`
- Day 23:
- Day 24:
- Day 25: