Skip to content

22

https://adventofcode.com/2024/day/22

Prob1

python
import sys

N = (1 << 24) - 1

sum = 0
for line in sys.stdin:
    x = int(line)

    for i in range(2000):
        x = ((x << 6) ^ x) & N
        x = ((x >> 5) ^ x) & N
        x = ((x << 11) ^ x) & N
    sum += x

print(sum)

Prob2

python
import sys

from typing import Dict, Tuple, List

N = (1 << 24) - 1


def next_rand(x):
    x = ((x << 6) ^ x) & N
    x = ((x >> 5) ^ x) & N
    x = ((x << 11) ^ x) & N
    return x


def seq_map(seed: int) -> Dict[Tuple[int, int, int, int], int]:
    prices = []
    x = seed
    for i in range(2001):
        prices.append(x % 10)
        x = next_rand(x)

    diff_prices = []
    for i in range(2000):
        diff_prices.append(prices[i+1] - prices[i])

    ret = {}
    for i in range(2000 - 4 + 1):
        key = (diff_prices[i],
               diff_prices[i+1],
               diff_prices[i+2],
               diff_prices[i+3])
        ret.setdefault(key, prices[i+4])
    return ret


sum = 0
seeds = []
for line in sys.stdin:
    x = int(line)
    seeds.append(x)

seed_seq_map: List[Dict[Tuple[int, int, int, int], int]] = []
keys = set()  # size <= 21**4
for s in seeds:
    seed_seq_map.append(seq_map(s))
    keys |= set(seed_seq_map[-1].keys())

print(len(keys))

ans = -123456789
for key in keys:
    price_sum = 0
    for smap in seed_seq_map:
        price_sum += smap.get(key, 0)
    if price_sum >= ans:
        ans = price_sum
        print(key, price_sum)

print(ans)

Changelog

Just observe 👀