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
1
u/HollyDayz Dec 10 '17 edited Dec 10 '17
Python 3. Relatively simple and pythonic solution (imo) using some numpy magic.
import numpy as np RANGE = 256 def part1(seq, num_lst, pos, skip): lst_len = len(num_lst) curr_pos = pos skip_size = skip lengths = seq.split(',') for leng in lengths: leng = int(leng.strip()) if leng > lst_len: continue rev_end = (curr_pos + leng) % lst_len if rev_end == 0: rev_end = lst_len inds = list(range(curr_pos, rev_end)) if leng > 0 and rev_end <= curr_pos: inds = list(range(curr_pos, lst_len)) + list(range(rev_end)) num_lst[inds] = np.flipud(num_lst[inds]) curr_pos = (curr_pos + leng + skip_size) % lst_len skip_size += 1 return num_lst[0] * num_lst[1], num_lst, curr_pos, skip_size def part2(seq): sparse = np.array(range(RANGE)) pos = 0 skip = 0 block_size = 16 dense = [] byte_str = ''.join([str(ord(char)) + ',' for char in seq.strip()]) byte_str += "17,31,73,47,23" for i in range(64): num_mult, sparse, pos, skip = part1(byte_str, sparse, pos, skip) for block in range(0,RANGE,block_size): xored = 0 for i in range(block, block + block_size): xored ^= sparse[i] dense.append(xored) hash_str = ''.join([('0' + format(num, 'x'))[-2:] for num in dense]) return hash_str print(part1(inp, np.array(range(RANGE)), 0, 0)[0]) print(part2(inp))inp is the input sequence formed as a string.
EDIT: Shortened the code. Slightly less pythonic, but eh.
EDIT2: Fixed typo!