-
Notifications
You must be signed in to change notification settings - Fork 1
/
25.py
65 lines (46 loc) · 1.42 KB
/
25.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
import fileinput
def parse():
grid = []
for line in fileinput.input():
grid.append(list(line.strip()))
return grid
grid = parse()
still_moving = True
steps = 0
while still_moving:
still_moving = False
new_grid = [row[:] for row in grid]
# move to east
for i, r in enumerate(grid):
for j, c in enumerate(r):
if c == '>':
row_to_check = i
col_to_check = j + 1 if j + 1 < len(r) else 0
# check if it can move
if grid[row_to_check][col_to_check] == '.':
new_grid[i][j] = '.'
new_grid[row_to_check][col_to_check] = '>'
still_moving = True
newer_grid = [row[:] for row in new_grid]
# move to south
for i, r in enumerate(new_grid):
for j, c in enumerate(r):
if c == 'v':
row_to_check = i + 1 if i + 1 < len(new_grid) else 0
col_to_check = j
# check if it can move
if new_grid[row_to_check][col_to_check] == '.':
newer_grid[i][j] = '.'
newer_grid[row_to_check][col_to_check] = 'v'
still_moving = True
grid = newer_grid
steps += 1
# print(steps)
# if steps == 1:
# for r in grid:
# print(''.join(r))
# break
print()
for r in grid:
print(''.join(r))
print(steps)