Watch
1
0
Fork
You've already forked advent-of-code
0

2017 Day 10 Part 2

This commit is contained in:
Jeremy Kaplan 2026-07-31 13:13:57 -04:00
commit 85f86147ee

View file

@ -1,13 +1,20 @@
from functools import reduce
with open("input/day10.txt") as f: with open("input/day10.txt") as f:
size, input = 256, f.read().strip() size, input = 256, f.read().strip()
lengths = [int(i) for i in input.split(",")]
ring = list(range(size)) def part1(size, input):
pos = 0 lengths = [int(i) for i in input.split(",")]
skip = 0
for length in lengths: ring = list(range(size))
_, _, ring = hash_round(0, 0, ring, lengths)
return ring[0] * ring[1]
def hash_round(pos, skip, ring, lengths):
for length in lengths:
assert length <= len(ring), "overlap undefined" assert length <= len(ring), "overlap undefined"
tail = min(length, len(ring) - pos) tail = min(length, len(ring) - pos)
@ -28,4 +35,28 @@ for length in lengths:
pos = (pos + length + skip) % len(ring) pos = (pos + length + skip) % len(ring)
skip += 1 skip += 1
print(ring[0] * ring[1]) return pos, skip, ring
def part2(size, input):
lengths = [ord(c) for c in input] + [17, 31, 73, 47, 23]
pos, skip = 0, 0
ring = list(range(size))
for _ in range(64):
pos, skip, ring = hash_round(pos, skip, ring, lengths)
dense = [0] * 16
for i in range(16):
start = 16 * i
block = ring[start : start + 16]
dense[i] = reduce(int.__xor__, block)
out = ""
for d in dense:
out += f"{d:02x}"
return out
print(part1(size, input))
print(part2(size, input))