-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.py
224 lines (170 loc) · 6.98 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from __future__ import annotations
from enum import Enum
from typing import Final, Iterable, TypeVar, Tuple
T = TypeVar('T')
class CycleData:
start_offset: int
period: int
values: list[int]
def __init__(self, start_offset: int, period: int, values: list[int]) -> None:
self.start_offset = start_offset
self.period = period
self.values = values
def __str__(self) -> str:
return f"start: {self.start_offset}, period: {self.period} | {self.values}"
class Field(Enum):
EMPTY = 0
ROCK = 1
BARRIER = 2
class Direction(Enum):
UP = 0
DOWN = 1
LEFT = 2
RIGHT = 3
class RockSystem:
array_2d: list[list[Field]]
cycle_data: CycleData | None
def __init__(self, lines: list[str] | None = None, array_2d: list[list[Field]] | None = None) -> None:
self.cycle_data = None
if array_2d:
# From 2D array
self.array_2d = array_2d
else:
self.array_2d = []
if not lines:
# Empty RockSystem object created
return
# Parse input
for line in lines:
line = line.strip()
self.array_2d.append([])
for c in line:
if c == "O":
self.array_2d[-1].append(Field.ROCK)
elif c == "#":
self.array_2d[-1].append(Field.BARRIER)
else:
self.array_2d[-1].append(Field.EMPTY)
def get_tilted(self, direction: Direction) -> RockSystem:
is_transposed: Final[bool] = direction in [Direction.UP, Direction.DOWN]
is_reversed: Final[bool] = direction in [Direction.UP, Direction.LEFT]
a2d: Final[list[list[Field]]] = transpose(self.array_2d) if is_transposed else self.array_2d
# For each row collect a list of groups.
# Each group counts the number of rocks
# and ends at a barrier, or at end of line.
groups: list[list[int]] = []
row_range: Iterable
for row in a2d:
buffer: int = 0
groups.append([])
if is_reversed:
row_range = reversed(range(len(row)))
else:
row_range = range(len(row))
for i in row_range:
if row[i] == Field.ROCK:
buffer += 1
if is_reversed:
at_end = i == 0
else:
at_end = i == len(row) - 1
if row[i] == Field.BARRIER or at_end:
groups[-1].append(buffer)
buffer = 0
# Build the resulting 2D array
WIDTH = len(a2d[0])
HEIGHT = len(a2d)
res: list[list[Field]] = [[Field.EMPTY for _ in range(WIDTH)] for _ in range(HEIGHT)]
for y in range(HEIGHT):
row_groups = groups.pop(0)
hit = 0
approaching = False
if is_reversed:
row_range = reversed(range(WIDTH))
else:
row_range = range(WIDTH)
for x in row_range:
# Determine if we are approaching the barrier / EOL
if is_reversed:
if x - row_groups[hit] < 0 or a2d[y][x - row_groups[hit]] == Field.BARRIER:
approaching = True
else:
if x + row_groups[hit] >= WIDTH or a2d[y][x + row_groups[hit]] == Field.BARRIER:
approaching = True
# Copy barriers, rest is empty
if a2d[y][x] == Field.BARRIER:
res[y][x] = Field.BARRIER
# Reset
approaching = False
# Next group
hit += 1
elif approaching:
# Place rocks if approaching
res[y][x] = Field.ROCK
return RockSystem(array_2d=(transpose(res) if is_transposed else res))
def get_load(self) -> int:
sum = 0
HEIGHT = len(self.array_2d)
for i in range(HEIGHT):
for el in self.array_2d[i]:
if el == Field.ROCK:
sum += HEIGHT - i
return sum
# Set window and cycles higher to avoid wrong results caused by collisions
def find_period(self, cycles: int, window: int) -> CycleData:
if self.cycle_data: # calculate this only once
return self.cycle_data
curr = self
previous: list[int] = []
for _ in range(cycles):
curr = curr.get_tilted(Direction.UP) \
.get_tilted(Direction.LEFT) \
.get_tilted(Direction.DOWN) \
.get_tilted(Direction.RIGHT)
load = curr.get_load()
previous.append(load)
for i in range(len(previous) - window):
for offset in range(i + window, len(previous) - window):
if previous[i:(i + window)] == previous[(i + offset):(i + window + offset)]:
cycle_data = CycleData(start_offset=i, period=offset, values=previous)
self.cycle_data = cycle_data
return cycle_data
raise Exception("Period not found")
def get_load_in_cycles(self, cycles: int) -> int:
if not self.cycle_data:
raise Exception("Call find_period() first")
if cycles <= self.cycle_data.start_offset + self.cycle_data.period:
return self.cycle_data.values[cycles - 1]
repeating_values = self.cycle_data.values[:-self.cycle_data.period]
return repeating_values[cycles % self.cycle_data.period - 1]
@staticmethod
def str_of_array_2d(a2d: list[list[Field]]) -> str:
res: str = ""
for row in a2d:
for c in row:
if c == Field.ROCK:
res += "O"
elif c == Field.BARRIER:
res += "#"
else:
res += "."
res += "\n"
return res
def __repr__(self) -> str:
return self.str_of_array_2d(self.array_2d)
def transpose(a2d: list[list[T]]) -> list[list[T]]:
return list(map(lambda x: list(x), zip(*a2d)))
def main() -> None:
lines = open("input.txt", "r").readlines()
rock_system = RockSystem(lines)
# Ex. 1
print("Ex. 1")
tilted_up = rock_system.get_tilted(Direction.UP)
print(tilted_up.get_load())
# Ex. 2
print("Ex. 2")
rock_system.find_period(cycles=10000, window=500)
CYCLES = 1000000000
print(rock_system.get_load_in_cycles(CYCLES))
if __name__ == "__main__":
main()