-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day22.py
80 lines (70 loc) · 1.53 KB
/
Day22.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
grid = []
with open('Day22_input') as f:
for l in f.readlines():
grid.append(list(l.strip()))
directions = {
'right': {
'up': (0,1, 'right'),
'right': (1,0, 'down'),
'down': (0,-1, 'left'),
'left': (-1,0, 'up'),
},
'left': {
'up': (0,-1, 'left'),
'left': (1,0, 'down'),
'down': (0,1, 'right'),
'right': (-1,0, 'up'),
},
'rev': {
'up': (1,0, 'down'),
'left': (0,1, 'right'),
'down': (-1,0, 'up'),
'right': (0,-1, 'left'),
},
'con': {
'up': (-1,0, 'up'),
'left': (0,-1, 'left'),
'down': (1,0, 'down'),
'right': (0,1, 'right'),
},
}
i=0
inf = 0
pos = (len(grid)//2, len(grid[0])//2)
curDir = 'up'
while(i<10000000):
# for l in grid:
# print(''.join(l))
# print('\n')
if (pos[0]) == len(grid):
grid.append(len(grid[0])*['.'])
elif (pos[0]) < 0:
grid.insert(0,len(grid[0])*['.'])
pos = (0, pos[1])
if (pos[1]) == len(grid[pos[0]]):
for l in grid:
l.append('.')
elif (pos[1]) < 0:
for l in grid:
l.insert(0, '.')
pos = (pos[0], 0)
# print(pos, len(grid), len(grid[pos[0]]))
if grid[pos[0]][pos[1]] == '.':
grid[pos[0]][pos[1]] = 'W'
turn = directions['left'][curDir]
elif grid[pos[0]][pos[1]] == 'W':
grid[pos[0]][pos[1]] = '#'
inf += 1
turn = directions['con'][curDir]
elif grid[pos[0]][pos[1]] == '#':
grid[pos[0]][pos[1]] = 'F'
turn = directions['right'][curDir]
elif grid[pos[0]][pos[1]] == 'F':
grid[pos[0]][pos[1]] = '.'
turn = directions['rev'][curDir]
curDir = turn[2]
pos = (pos[0]+turn[0], pos[1]+turn[1])
i+=1
print(inf)
# for l in grid:
# print(''.join(l))