Created
December 10, 2017 06:24
-
-
Save PamelaM/2da6e6ecdaa26a40346d6be3f47aa406 to your computer and use it in GitHub Desktop.
Advent of Code 2017, Day 10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INPUT = """165,1,255,31,87,52,24,113,0,91,148,254,158,2,73,153""" | |
import asciitree | |
from collections import OrderedDict, defaultdict | |
def _replace(data, position, chunk): | |
#print position, chunk | |
for idx, v in enumerate(chunk): | |
data[position+idx] = v | |
def hash_list(lengths, data, position=0, skip_size=0): | |
#print position, skip_size, data | |
base_len = len(data) | |
for length in lengths: | |
#print length | |
data = data | |
end = position+length | |
if end >= base_len: | |
data = data + data | |
chunk = data[position:end] | |
#print position, end, chunk, '->', | |
chunk.reverse() | |
#print chunk | |
if end >= base_len: | |
data = data[:base_len] | |
before_len = base_len - position | |
after_len = length - before_len | |
_replace(data, position, chunk[:before_len]) | |
_replace(data, 0, chunk[before_len:]) | |
else: | |
_replace(data, position, chunk) | |
position += (length + skip_size) | |
if position >= base_len: | |
position = position % base_len | |
skip_size += 1 | |
#print position, skip_size #, data | |
return data, position, skip_size | |
def part_one(lengths, base_len): | |
data = list(range(base_len)) | |
position = 0 | |
skip_size = 0 | |
data, position, skip_size = hash_list( | |
(int(n) for n in lengths.split(",")), | |
data, | |
0, | |
0, | |
) | |
return data[0]*data[1] | |
print part_one("3,4,1,5", 5) | |
print part_one(INPUT, 256) | |
def part_two(lengths): | |
lengths = [ord(c) for c in lengths] + [17, 31, 73, 47, 23] | |
data = list(range(256)) | |
position = 0 | |
skip_size = 0 | |
for __ in range(64): | |
data, position, skip_size = hash_list(lengths, data, position, skip_size) | |
dense = [] | |
for offset in range(0, 256, 16): | |
chunk = data[offset:offset+16] | |
d = chunk[0] | |
for v in chunk[1:]: | |
d = d ^ v | |
dense.append(d) | |
return "".join("%02x" % d for d in dense) | |
print part_two(INPUT) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment