-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.py
56 lines (53 loc) · 1.43 KB
/
day06.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
from fileinput import input
import os, math
agent = {
'position': None,
'direction': None
}
directions = {
'v': {
'vector': (1,0),
'turn': '<',
'marker': '|'
},
'<': {
'vector': (0,-1),
'turn': '^',
'marker': '-'
},
'^': {
'vector': (-1,0),
'turn': '>',
'marker': '|'
},
'>': {
'vector': (0,1),
'turn': 'v',
'marker': '-'
}
}
map, i = [], 1
for line in input():
line = line.replace('\n','')
for symbol in directions.keys():
if symbol in line:
agent['direction'], agent['position'] = symbol, (i, line.index(symbol))
map.append(line)
i+=1
continuer, count = True, 1
while continuer:
x, y = agent['position']
next_y, next_x = y + directions[agent['direction']]['vector'][0], x + directions[agent['direction']]['vector'][1]
if next_y < 0 or next_x < 0 or next_y >= len(map) or next_x >= len(map[0]):
continuer = False
elif map[next_y][next_x] == '#':
map[y]= map[y][:x] + '+' + map[next_y][x + 1:]
agent['direction'] = directions[agent['direction']]['turn']
else:
if map[next_y][next_x] == '.':
map[next_y] = map[next_y][:next_x] + directions[agent['direction']]['marker'] + map[next_y][next_x + 1:]
count += 1
agent['position'] = (next_y, next_x)
print('---')
[print(l) for l in map]
print(agent, count)