day 22, optimized for codon

This commit is contained in:
setop 2024-12-24 00:52:05 +01:00
parent 6081f917c0
commit 2cb6b3a793
2 changed files with 43 additions and 0 deletions

15
d22/part1.py Normal file
View File

@ -0,0 +1,15 @@
import sys
mask = 16777216-1
def ng(n:int):
n ^= (n << 6) & mask
n ^= (n >> 5) & mask
n ^= (n << 11) & mask
return n
ans = 0
for x in = map(int,sys.stdin.read().strip().split('\n')):
for _ in range(2000):
x = ng(x)
ans += x
print(ans)

28
d22/part2.py Normal file
View File

@ -0,0 +1,28 @@
import sys
mask = 16777216-1
def ng(n:int) -> int:
n ^= (n << 6) & mask
n ^= (n >> 5) & mask
n ^= (n << 11) & mask
return n
ranges = dict()
for seed in map(int,sys.stdin.read().strip().split('\n')):
visited = set()
pseed = seed % 10
key = 0
for i in range(2000):
nextSeed = ng(seed)
nseed = nextSeed %10
change = 9 + nseed - pseed
key = (key * 19 + change) % 130321
if i>2:
if key not in visited:
if key not in ranges:
ranges[key] = 0
ranges[key] += nseed
visited.add(key)
seed = nextSeed
pseed = nseed
print(max(ranges.values()))