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 2017/14

+65
+62
2017/day14.py
··· 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) 32 + 33 + 34 + KEY_STRING = fileinput.input()[0].strip() 35 + DISK = set() 36 + 37 + for x in range(128): 38 + hash_hex = knot_hash(KEY_STRING + '-' + str(x)) 39 + hash_bits = ''.join('{0:04b}'.format(int(x, 16)) for x in hash_hex) 40 + for y, b in enumerate(hash_bits): 41 + if b == '1': 42 + DISK.add(Point(x, y)) 43 + 44 + regions = 0 45 + seen = set() 46 + 47 + for p in DISK: 48 + if p in seen: 49 + continue 50 + 51 + queue = [p] 52 + while queue: 53 + np = queue.pop() 54 + seen.add(np) 55 + for n in np.neighbours_4(): 56 + if n in DISK and n not in seen: 57 + queue.append(n) 58 + 59 + regions += 1 60 + 61 + print "Number of used squares:", len(DISK) 62 + print "Number of regions:", regions
+1
2017/inputs/14.txt
··· 1 + nbysizxe
+2
2017/outputs/14.txt
··· 1 + 8216 2 + 1139