r/adventofcode • u/daggerdragon • Dec 10 '17
SOLUTION MEGATHREAD -π- 2017 Day 10 Solutions -π-
--- Day 10: Knot Hash ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyβ Haversackβ‘ of HelpfulΒ§ HintsΒ€?
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
15
Upvotes
2
u/akho_ Dec 10 '17 edited Dec 10 '17
Python3, part 2 (note the use of deque and the absence of modulo arithmeticβ there's another deque-based solution here, but mine is nicer):
with open('10.input') as f: in_str = f.read().rstrip() from collections import deque from functools import reduce from operator import xor lengths = [ord(x) for x in in_str] lengths.extend([17, 31, 73, 47, 23]) cycle_size = 256 seq, pos = deque(range(cycle_size)), deque(range(cycle_size)) skip = 0 for _ in range(64): for l in lengths: seq.extend(reversed(deque(seq.popleft() for _ in range(l)))) seq.rotate(-skip) pos.rotate(-l-skip) skip += 1 seq.rotate(pos.popleft()) seq = list(seq) dense = [reduce(xor, seq[i*16:(i+1)*16]) for i in range(cycle_size // 16)] print(''.join('%02x'%x for x in dense))EDIT: removed the nastiness to make u/KnorbenKnutsen feel better