-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday25.py
executable file
·81 lines (62 loc) · 1.32 KB
/
day25.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
#!/usr/bin/env python3
from utils.all import *
advent.setup(2021, 25)
DEBUG = 'debug' in map(str.lower, sys.argv)
if not DEBUG:
fin = advent.get_input()
else:
fin = io.StringIO('''\
v...>>.vv>
.vv>>.vv..
>>.>v>...v
>>v>>.>.v.
v>v.vv.v..
>.>>..v...
.vv..>.>v.
v.v..>>v.v
....v..v.>
''')
mat = read_char_matrix(fin)
def step(mat):
n = 0
while 1:
done = True
w = len(mat[0])
h = len(mat)
newmat = [['.' for _ in range(w)] for _ in range(h)]
for r, row in enumerate(mat):
for c, cell in enumerate(row):
if cell == '>':
rr, cc = r, (c + 1) % w
# eprint(r, c, cell, '|', rr, cc, mat[r][(c + 1) % w])
if mat[rr][cc] == '.':
newmat[rr][cc] = '>'
done = False
else:
newmat[r][c] = '>'
elif cell == 'v':
newmat[r][c] = cell
mat = newmat
newmat = [['.' for _ in range(w)] for _ in range(h)]
for r, row in enumerate(mat):
for c, cell in enumerate(row):
if cell == 'v':
rr, cc = (r + 1) % h, c
# eprint(r, c, cell, '|', rr, cc, mat[rr][cc])
if mat[rr][cc] == '.':
newmat[rr][cc] = 'v'
done = False
else:
newmat[r][c] = cell
elif cell == '>':
newmat[r][c] = cell
n += 1
mat = newmat
# eprint(n)
# dump_char_matrix(mat)
# eprint('')
if done:
break
return n
ans = step(mat)
advent.print_answer(1, ans)