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.

Update day20.py

+17 -21
+17 -21
2019/day20.py
··· 2 2 from string import ascii_uppercase 3 3 from collections import defaultdict 4 4 5 - from utils import Point, DIRS 5 + from utils import Point 6 6 7 7 8 8 MAZE = defaultdict(lambda: '#') ··· 28 28 # Determine the true label name and grid position of portals 29 29 for p, c in MAZE.items(): 30 30 if c in ascii_uppercase: 31 - name = '' 32 - if MAZE[p + Point(-1, 0)] in ascii_uppercase: 33 - name = MAZE[p + Point(-1, 0)] + c 34 - op = p + Point(-1, 0) 35 - elif MAZE[p + Point(1, 0)] in ascii_uppercase: 36 - name = c + MAZE[p + Point(1, 0)] 37 - op = p + Point(1, 0) 38 - elif MAZE[p + Point(0, -1)] in ascii_uppercase: 39 - name = MAZE[p + Point(0, -1)] + c 40 - op = p + Point(0, -1) 41 - elif MAZE[p + Point(0, 1)] in ascii_uppercase: 42 - name = c + MAZE[p + Point(0, 1)] 43 - op = p + Point(0, 1) 44 - 45 31 for np in p.neighbours_4(): 46 - if MAZE[np] == '.': 47 - portal_loc = np 48 - for np in op.neighbours_4(): 49 - if MAZE[np] == '.': 50 - portal_loc = np 32 + if MAZE[np] in ascii_uppercase: 33 + if np.x > p.x or np.y > p.y: 34 + name = c + MAZE[np] 35 + else: 36 + name = MAZE[np] + c 37 + 38 + break 39 + 40 + for pp in p.neighbours_4(): 41 + if MAZE[pp] == '.': 42 + portal_loc = pp 43 + for pp in np.neighbours_4(): 44 + if MAZE[pp] == '.': 45 + portal_loc = pp 51 46 52 47 if name == 'AA': 53 48 AA = portal_loc ··· 58 53 59 54 # Map portal positions and track inner/outer edges 60 55 for x in PORTALS: 61 - a, b = list(PORTALS[x]) 56 + a, b = PORTALS[x] 62 57 PORTAL_MAP[a] = b 63 58 PORTAL_MAP[b] = a 64 59 if a.x == 2 or a.x == (MAX_X - 2) or a.y == 2 or a.y == (MAX_Y - 2): ··· 81 76 if p in PORTAL_MAP: 82 77 if not (p in OUTERS and level == 0) or not recursive: 83 78 neighbours.append(PORTAL_MAP[p]) 79 + 84 80 for np in neighbours: 85 81 new_level = level 86 82 if recursive: