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 55 lines 1.1 kB view raw
1import fileinput 2from utils import Point 3 4KEYPAD_1 = [ 5 ['1', '2', '3'], 6 ['4', '5', '6'], 7 ['7', '8', '9'], 8] 9 10KEYPAD_2 = [ 11 ['_', '_', '1', '_', '_'], 12 ['_', '2', '3', '4', '_'], 13 ['5', '6', '7', '8', '9'], 14 ['_', 'A', 'B', 'C', '_'], 15 ['_', '_', 'D', '_', '_'], 16] 17 18DIRS = { 19 'U': Point(0, -1), 20 'D': Point(0, 1), 21 'L': Point(-1, 0), 22 'R': Point(1, 0), 23} 24 25 26def move(keypad, pos, d): 27 """Returns the (x, y) coordinate after (possibly) moving.""" 28 new = pos + DIRS[d] 29 30 if new.x < 0 or new.y < 0: 31 return pos 32 elif new.x >= len(keypad[0]) or new.y >= len(keypad): 33 return pos 34 elif keypad[new.y][new.x] == '_': 35 return pos 36 37 return new 38 39 40pos_1 = Point(1, 1) # start in middle 41pos_2 = Point(0, 2) # start in middle-left 42 43code_1 = '' 44code_2 = '' 45 46for line in fileinput.input(): 47 for c in line.strip(): 48 pos_1 = move(KEYPAD_1, pos_1, c) 49 pos_2 = move(KEYPAD_2, pos_2, c) 50 51 code_1 += KEYPAD_1[pos_1.y][pos_1.x] 52 code_2 += KEYPAD_2[pos_2.y][pos_2.x] 53 54print "Theoretical bathroom code: {}".format(code_1) 55print "Actual bathroom code: {}".format(code_2)