-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path02_Bathroom_Security.py
69 lines (53 loc) · 1.34 KB
/
02_Bathroom_Security.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
from aoc import *
KEYPAD1 = list(filter(None, '''
123
456
789
'''.splitlines()))
KEYPAD2 = list(filter(None, '''
1
234
56789
ABC
D
'''.splitlines()))
instructions = read()
cur = Vec2(1, 1)
code = []
for i in instructions:
for c in i:
tmp = cur.step(c)
if 0 <= tmp.x <= 2 and 0 <= tmp.y <= 2:
cur = tmp
code.append(KEYPAD1[cur.y][cur.x])
print('Star 1:', ''.join(code))
cur = Vec2(2, 2)
code = []
for i in instructions:
for c in i:
tmp = cur.step(c)
if (tmp-2).mdist() <= 2:
cur = tmp
code.append(KEYPAD2[cur.y][cur.x])
print('Star 2:', ''.join(code))
### Alternative solution using just complex numbers ###
cur = 1 + 1j
code = []
for i in instructions:
for c in i:
delta = -1 if c == 'L' else 1 if c == 'R' else -1j if c == 'U' else 1j
tmp = cur + delta
if 0 <= tmp.real <= 2 and 0 <= tmp.imag <= 2:
cur = tmp
code.append(KEYPAD1[int(cur.imag)][int(cur.real)])
print('Star 1 (complex):', ''.join(code))
cur = 2 + 2j
code = []
for i in instructions:
for c in i:
delta = -1 if c == 'L' else 1 if c == 'R' else -1j if c == 'U' else 1j
tmp = cur + delta
if abs(tmp.real-2) + abs(tmp.imag-2) <= 2:
cur = tmp
code.append(KEYPAD2[int(cur.imag)][int(cur.real)])
print('Star 2 (complex):', ''.join(code))