-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday22-Monkey Map.py
79 lines (74 loc) · 1.76 KB
/
day22-Monkey Map.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
f = open('input')
lines = f.readlines()
# lines = [_.strip() for _ in lines]
first = None
mat = []
N = len(lines[0].replace('\n', ''))
print(N)
for j, line in enumerate(lines):
if line == '\n':
MOVES = lines[j + 1]
break
row = []
line = line.ljust(N+1)
for i, ch in enumerate(line):
if ch != '\n':
if ch == '.' and not first:
first = (i, j)
row.append(ch)
mat.append(''.join(row))
direction = (1, 0)
TURN = {
(1, 0): {'R': (0, 1),
'L': (0, -1)
},
(0, 1): {'R': (-1, 0),
'L': (1, 0)
},
(-1, 0): {'R': (0, -1),
'L': (0, 1)
},
(0, -1): {'R': (1, 0),
'L': (-1, 0)
},
}
# pprint(mat)
print('start at ', first)
def move(point, direction, step):
x, y = point
dx, dy = direction
prevOk = (x, y)
while step > 0:
print('\t', x, y)
x += dx
y += dy
if x == len(mat[0]):
x = 0
elif x < 0:
x = len(mat[0]) - 1
elif y == len(mat):
y = 0
elif y < 0:
y = len(mat) - 1
if mat[y][x] == '#':
return prevOk
if mat[y][x] == ' ':
continue
step -= 1
prevOk = (x, y)
return x, y
s = ''
point = first
for ch in MOVES + 'L':
if ch in 'RL':
step = int(s)
lastDir = direction
print('move:', direction, step, 'then turn:', TURN[direction][ch])
point = move(point, direction, step)
print('point', point)
s = ''
direction = TURN[direction][ch]
else:
s += ch
print(point, lastDir)
print('part1', (point[0] + 1) * 4 + (point[1] + 1) * 1000 + list(TURN).index(lastDir))