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 solution for 2016/17

+42
+41
2016/day17.py
··· 1 + import fileinput 2 + from hashlib import md5 3 + from itertools import compress 4 + 5 + 6 + DIRS = { 7 + 'U': (0, -1), 8 + 'D': (0, 1), 9 + 'L': (-1, 0), 10 + 'R': (1, 0), 11 + } 12 + 13 + 14 + def open_doors(digest): 15 + """Returns a 4-tuple of booleans representing whether doors UDLR are open.""" 16 + return [x in 'bcdef' for x in digest[:4]] 17 + 18 + 19 + def pathfind(salt): 20 + horizon = [(0, 0, '')] 21 + 22 + while horizon: 23 + x, y, path = horizon.pop(0) 24 + digest = md5(salt + path).hexdigest() 25 + 26 + for door in compress('UDLR', open_doors(digest)): 27 + dx, dy = DIRS[door] 28 + nx, ny = x + dx, y + dy 29 + 30 + if (0 <= nx < 4) and (0 <= ny < 4): 31 + if nx == 3 and ny == 3: 32 + yield path + door 33 + else: 34 + horizon.append((nx, ny, path + door)) 35 + 36 + 37 + if __name__ == '__main__': 38 + salt = fileinput.input()[0].strip() 39 + paths = sorted(pathfind(salt), key=len) 40 + print 'Shortest path:', paths[0] 41 + print 'Longest path length:', len(paths[-1])
+1
2016/input17.txt
··· 1 + yjjvjgan