-
Notifications
You must be signed in to change notification settings - Fork 0
/
02.py
50 lines (34 loc) · 834 Bytes
/
02.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
from lib import *
input = read_input(2016, 2)
out = []
x = y = 1
for line in input.splitlines():
for c in line:
if c == "L" and x > 0:
x -= 1
elif c == "R" and x < 2:
x += 1
elif c == "U" and y > 0:
y -= 1
elif c == "D" and y < 2:
y += 1
out.append(y * 3 + x + 1)
print(*out, sep="")
out = ""
x, y = 0, 2
keypad = [" 1 ", " 234 ", "56789", " ABC ", " D "]
for line in input.splitlines():
for c in line:
nx, ny = x, y
if c == "L":
nx -= 1
elif c == "R":
nx += 1
elif c == "U":
ny -= 1
elif c == "D":
ny += 1
if 0 <= nx < 5 and 0 <= ny < 5 and keypad[ny][nx] != " ":
x, y = nx, ny
out += keypad[y][x]
print(out)