-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvent2016_2.py
93 lines (78 loc) · 1.98 KB
/
advent2016_2.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
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
# http://adventofcode.com/2016
from itertools import cycle
import logging as log
log.basicConfig(level=log.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
number_pad = {
(-1, 1): "1",
(0, 1): "2",
(1, 1): "3",
(-1, 0): "4",
(0, 0): "5",
(1, 0): "6",
(-1, -1): "7",
(0, -1): "8",
(1, -1): "9",
}
number_pad2 = {
(0, 2): "1",
(-1, 1): "2",
(0, 1): "3",
(1, 1): "4",
(-2, 0): "5",
(-1, 0): "6",
(0, 0): "7",
(1, 0): "8",
(2, 0): "9",
(-1, -1): "A",
(0, -1): "B",
(1, -1): "C",
(0, -2): "D",
}
number_pad_reversed2 = {k: v for v, k in number_pad.items()}
def process_move2(xstart, ystart, direction):
x, y = xstart, ystart
if direction == "U":
if y < 2 and x == 0:
y += 1
elif y < 1 and x in (-1, 1):
y += 1
elif direction == "D":
if y > -2 and x == 0:
y -= 1
elif y > -1 and x in (-1, 1):
y -= 1
elif direction == "R":
if x < 2 and y == 0:
x += 1
elif x < 1 and y in (-1, 1):
x += 1
elif direction == "L":
if x > -2 and y == 0:
x -= 1
elif x > -1 and y in (-1, 1):
x -= 1
return x, y, number_pad2[(x, y)]
def process_move(xstart, ystart, direction):
x, y = xstart, ystart
if direction == "U":
if y < 1:
y += 1
elif direction == "D":
if y > -1:
y -= 1
elif direction == "R":
if x < 1:
x += 1
elif direction == "L":
if x > -1:
x -= 1
return x, y, number_pad[(x, y)]
if __name__ == "__main__":
x, y = -2, 0
num = ""
with open("advent2016-2_input.txt") as file:
for line in file:
for character in line:
x, y, num = process_move2(x, y, character)
log.debug(num)