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.

at main 41 lines 793 B view raw
1import fileinput 2from utils import Point, DIRS 3 4 5DIAGRAM = {} 6 7for y, line in enumerate(fileinput.input()): 8 for x, c in enumerate(line.strip('\n')): 9 if y == 0 and c == '|': 10 start = Point(x, y) 11 12 DIAGRAM[Point(x, y)] = c 13 14d = 0 15pos = start 16 17steps = 0 18letters = [] 19 20while True: 21 tile = DIAGRAM.get(pos, ' ') 22 23 if tile.isalpha(): 24 letters.append(tile) 25 26 elif tile == '+': 27 # Determine whether to turn left or right 28 right_tile = DIAGRAM.get(pos + DIRS[(d + 1) % 4], ' ') 29 if right_tile == ' ': 30 d = (d - 1) % 4 31 else: 32 d = (d + 1) % 4 33 34 elif tile == ' ': 35 break 36 37 pos = pos + DIRS[d] 38 steps += 1 39 40print "Letters seen in path:", ''.join(letters) 41print "Total number of steps:", steps