-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.py
80 lines (60 loc) · 1.49 KB
/
day2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
import get_input
day = int(os.path.basename(__file__).replace('day', '').replace('.py', ''))
data = get_input.get_input(day)
lines = data.split('\n')
# y, x
pos = [1, 1]
# o-> x
# |
# v
# y
# Keypad
keypad = [list('123'),
list('456'),
list('789')]
# keypad[y][x]
combination = []
for line in lines:
for char in line:
if char == 'U':
pos[0] = max(0, pos[0] - 1)
elif char == 'D':
pos[0] = min(2, pos[0] + 1)
elif char == 'L':
pos[1] = max(0, pos[1] - 1)
elif char == 'R':
pos[1] = min(2, pos[1] + 1)
else:
print('ERROR')
combination.append(keypad[pos[0]][pos[1]])
print('Combination: {}'.format(''.join(combination)))
# 1
# 2 3 4
# 5 6 7 8 9
# A B C
# D
combination = []
keypad = [list('__1__'),
list('_234_'),
list('56789'),
list('_ABC_'),
list('__D__')]
pos = [2, 0]
for line in lines:
for char in line:
old_pos = pos.copy()
if char == 'U':
pos[0] = max(0, pos[0] - 1)
elif char == 'D':
pos[0] = min(4, pos[0] + 1)
elif char == 'L':
pos[1] = max(0, pos[1] - 1)
elif char == 'R':
pos[1] = min(4, pos[1] + 1)
else:
print('ERROR')
if keypad[pos[0]][pos[1]] == '_':
pos = old_pos
combination.append(keypad[pos[0]][pos[1]])
print('Actual Combination: {}'.format(''.join(combination)))