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.

Add knot_hash() to utils.py

+30 -1
+1 -1
2017/starter.py
··· 6 6 from collections import Counter, deque, namedtuple # NOQA 7 7 from itertools import count, product, permutations, combinations, combinations_with_replacement # NOQA 8 8 9 - from utils import (parse_line, mul, all_unique, factors, memoize, primes, new_table, md5, sha256, # NOQA 9 + from utils import (parse_line, mul, all_unique, factors, memoize, primes, new_table, md5, sha256, knot_hash, # NOQA 10 10 Point, DIRS, DIRS_4, DIRS_8) # NOQA 11 11 12 12 # Itertools Functions:
+29
2017/utils.py
··· 96 96 return s.hexdigest() 97 97 98 98 99 + def knot_hash(msg): 100 + lengths = [ord(x) for x in msg] + [17, 31, 73, 47, 23] 101 + sparse = range(0, 256) 102 + pos = 0 103 + skip = 0 104 + 105 + for _ in range(64): 106 + for l in lengths: 107 + for i in range(l // 2): 108 + x = (pos + i) % len(sparse) 109 + y = (pos + l - i - 1) % len(sparse) 110 + sparse[x], sparse[y] = sparse[y], sparse[x] 111 + 112 + pos = pos + l + skip % len(sparse) 113 + skip += 1 114 + 115 + sparse = sparse 116 + dense = [] 117 + 118 + for i in range(16): 119 + res = 0 120 + for j in range(0, 16): 121 + res ^= sparse[(i * 16) + j] 122 + 123 + dense.append(res) 124 + 125 + return ''.join('%02x' % x for x in dense) 126 + 127 + 99 128 @total_ordering 100 129 class Point: 101 130 """Simple 2-dimensional point."""