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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# pyright: strict
from collections.abc import Callable
from enum import Enum, auto
from itertools import chain, count
from sys import stdin
from typing import Self, Type
class Dir(Enum):
NORTH = auto()
EAST = auto()
SOUTH = auto()
WEST = auto()
class Tile(Enum):
AIR = auto()
ROUND = auto()
CUBE = auto()
@staticmethod
def from_str(s: str) -> "Tile":
match s:
case ".":
return Tile.AIR
case "O":
return Tile.ROUND
case "#":
return Tile.CUBE
case _:
raise ValueError
def __str__(self) -> str:
match self:
case Tile.AIR:
return "."
case Tile.ROUND:
return "O"
case Tile.CUBE:
return "#"
class Platform:
platform: list[Tile]
width: int
height: int
def __init__(self, state: tuple[tuple[Tile, ...], ...]):
self.platform = list(chain.from_iterable(state))
self.width = len(state[0])
self.height = len(state)
def settle(self, direction: Dir):
xlate: Callable[[int, int], int]
if direction in {Dir.NORTH, Dir.SOUTH}:
lines = self.width
line_length = self.height
if direction == Dir.NORTH:
xlate = lambda l, i: l + i * self.width
else:
xlate = lambda l, i: l + (self.height - i - 1) * self.width
else:
lines = self.height
line_length = self.width
if direction == Dir.EAST:
xlate = lambda l, i: l * self.width + i
else:
xlate = lambda l, i: l * self.width + (self.width - i - 1)
for line in range(lines):
floor = 0
for i in range(line_length):
pos = xlate(line, i)
if self.platform[pos] == Tile.ROUND:
self.platform[xlate(line, floor)], self.platform[pos] = (
self.platform[pos],
self.platform[xlate(line, floor)],
)
floor += 1
elif self.platform[pos] == Tile.CUBE:
floor = i + 1
@property
def load(self) -> int:
return sum(
self.height - y
for y in range(self.height)
for x in range(self.width)
if self.platform[x + self.width * y] == Tile.ROUND
)
def cycle(self):
self.settle(Dir.NORTH)
self.settle(Dir.EAST)
self.settle(Dir.SOUTH)
self.settle(Dir.WEST)
@classmethod
def from_str(cls: Type[Self], s: str) -> Self:
return cls(tuple(tuple(Tile.from_str(c) for c in l) for l in s.split("\n")))
def __str__(self) -> str:
return "\n".join(
"".join(map(str, self.platform[p : p + self.width]))
for p in range(0, self.width * self.height, self.width)
)
def __hash__(self) -> int:
return hash((self.width, self.height) + tuple(self.platform))
p = Platform.from_str(stdin.read().rstrip())
past: dict[int, int] = dict()
past[hash(p)] = 0
p.settle(Dir.NORTH)
print(p.load)
for i in count(1):
p.cycle()
h = hash(p)
if h in past:
cycle_start = past[h]
cycle_length = i - past[h]
break
past[hash(p)] = i
else:
raise ValueError
for i in range((1000000000 - cycle_start) % cycle_length):
p.cycle()
print(p.load)
|