My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Use utils.knot_hash in 2017/14 solution

+4 -34
+1 -30
2017/day14.py
··· 1 1 import fileinput 2 - from utils import Point 3 - 4 - 5 - def knot_hash(msg): 6 - lengths = [ord(x) for x in msg] + [17, 31, 73, 47, 23] 7 - sparse = range(0, 256) 8 - pos = 0 9 - skip = 0 10 - 11 - for _ in range(64): 12 - for l in lengths: 13 - for i in range(l // 2): 14 - x = (pos + i) % len(sparse) 15 - y = (pos + l - i - 1) % len(sparse) 16 - sparse[x], sparse[y] = sparse[y], sparse[x] 17 - 18 - pos = pos + l + skip % len(sparse) 19 - skip += 1 20 - 21 - sparse = sparse 22 - dense = [] 23 - 24 - for i in range(16): 25 - res = 0 26 - for j in range(0, 16): 27 - res ^= sparse[(i * 16) + j] 28 - 29 - dense.append(res) 30 - 31 - return ''.join('%02x' % x for x in dense) 2 + from utils import Point, knot_hash 32 3 33 4 34 5 KEY_STRING = fileinput.input()[0].strip()
+3 -4
2017/utils.py
··· 122 122 pos = pos + l + skip % len(sparse) 123 123 skip += 1 124 124 125 - sparse = sparse 126 - dense = [] 125 + hash_val = 0 127 126 128 127 for i in range(16): 129 128 res = 0 130 129 for j in range(0, 16): 131 130 res ^= sparse[(i * 16) + j] 132 131 133 - dense.append(res) 132 + hash_val += res << ((16 - i - 1) * 8) 134 133 135 - return ''.join('%02x' % x for x in dense) 134 + return '%032x' % hash_val 136 135 137 136 138 137 @total_ordering