-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path14.py
34 lines (26 loc) · 1.01 KB
/
14.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
from sys import stdin
DIRS = [(-1, 0), (0, -1), (1, 0), (0, 1)]
lines = [list(line.strip()) for line in stdin]
Y, X = len(lines), len(lines[0])
coords = [(y, x) for y in range(Y) for x in range(X)]
def find_o_coords():
return [(y, x) for y, x in coords if lines[y][x] == 'O']
seen = {}
for i in range(1, 10000):
for yd, xd in DIRS:
for y, x in find_o_coords():
ybest, xbest = yo, xo = y, x
while 0 <= y < Y and 0 <= x < X and lines[y][x] != '#':
if lines[y][x] == '.':
ybest, xbest = y, x
y, x = y+yd, x+xd
lines[yo][xo], lines[ybest][xbest] = lines[ybest][xbest], lines[yo][xo]
load_on_beams = sum(Y-y for y, _ in find_o_coords())
if i == 1 and (yd, xd) == DIRS[0]:
print(load_on_beams)
if str(lines) in seen:
repeat_interval = i - seen[str(lines)]
if (1_000_000_000 - i) % repeat_interval == 0:
print(load_on_beams)
exit()
seen[str(lines)] = i