-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.py
57 lines (46 loc) · 1.22 KB
/
solution.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
inputs = open("input.txt", "r").readlines()
def calc(grid, t=0):
return sum(
[0, i + (t := (t + 1))][c.strip() == "O"]
for i, chunk in enumerate(grid[::-1])
for c in sorted(chunk)
)
print(
sum(
[
calc(line)
for line in list(map(lambda x: "".join(x).split("#"), zip(*inputs)))
]
)
)
grid = tuple(map(lambda x: x.strip(), inputs))
total_cycles = 1000000000
cycles = [grid]
def slide_nwse(grid):
for _ in range(4):
grid = tuple(
"#".join("".join(sorted(c, reverse=True)) for c in chunk.split("#"))
for chunk in tuple(map("".join, zip(*grid)))[::-1]
)
grid = tuple(
[
"".join(row[::-1])
for row in zip(*tuple(map(lambda x: "".join(x[::-1]), zip(*grid))))
]
)
return grid
for i in range(total_cycles):
if (grid := slide_nwse(grid)) in cycles:
break
cycles.append(grid)
print(
sum(
s.count("O") * (len(grid) - j)
for j, s in enumerate(
cycles[
cycles.index(grid)
+ (total_cycles - cycles.index(grid)) % (i + 1 - cycles.index(grid))
]
)
)
)