-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday14.py
More file actions
182 lines (134 loc) · 4.23 KB
/
day14.py
File metadata and controls
182 lines (134 loc) · 4.23 KB
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
from dataclasses import dataclass
from pathlib import Path
from typing import Literal
from loguru import logger
from typer import Typer
from utils import timer
main = Typer()
@dataclass
class Grid:
"""Game board abstraction"""
grid: list[list[str]]
@classmethod
def from_input(cls, input: str) -> "Grid":
"""Parse string input"""
return cls([list(line.strip()) for line in input.splitlines()])
@property
def n_rows(self) -> int:
return len(self.grid)
@property
def n_cols(self) -> int:
return len(self.grid[0])
@property
def total_load(self) -> int:
return sum(
(self.n_rows - i) * sum(symbol == "O" for symbol in line)
for i, line in enumerate(self.grid)
)
@staticmethod
def move_line(line: list[str]):
"""Move rocks along a single line
Operates inplace
Args:
line: List of ".", "#", and/or "O".
"""
free_pos: int | None = None
for i, symbol in enumerate(line):
if symbol == "#":
free_pos = None
continue
if symbol == ".":
if free_pos is None:
free_pos = i
continue
# symbol == 'O'
if free_pos is None:
continue
line[free_pos] = "O"
line[i] = "."
free_pos += 1
def tilt(self, direction: Literal["N", "E", "S", "W"] = "N"):
"""Tilt the board in the given direction
Args:
direction: Either "N", "E", "S", or "W". Defaults to "N".
"""
# TODO get rid of the whole transpose and double reverse stuff
if direction == "W":
for row in self.grid:
self.move_line(row)
return
if direction == "E":
for i, row in enumerate(self.grid):
row.reverse()
self.move_line(row)
row.reverse()
self.grid[i] = row
return
if direction == "N":
transposed_grid = []
for col in zip(*self.grid):
col = list(col)
self.move_line(col)
transposed_grid.append(col)
self.grid = list(list(row) for row in zip(*transposed_grid))
return
transposed_grid = []
for col in zip(*self.grid):
col = list(reversed(col))
self.move_line(col)
col.reverse()
transposed_grid.append(col)
self.grid = list(list(row) for row in zip(*transposed_grid))
def spin_cycle(self):
"""Tilt the board in all four directions
Counterclockwise. Starting from North.
"""
self.tilt("N")
self.tilt("W")
self.tilt("S")
self.tilt("E")
def __str__(self) -> str:
return "\n".join("".join(row) for row in self.grid)
@timer
def task01(input: str) -> int:
"""Solution for task01
Args:
input: Input string of the board
Returns:
Total load after tilt to North
"""
grid = Grid.from_input(input)
grid.tilt()
return grid.total_load
@timer
def task02(input: str, n_cycles: int = 1_000_000_000) -> int:
"""Solution for task02
Args:
input: Input string of the board
n_cycles: How often to spin the board. Defaults to 1_000_000_000.
Returns:
Total load after n_cycles
"""
grid = Grid.from_input(input)
state_dict: dict[str, int] = {str(grid): 0}
lookup: list[str] = [str(grid)]
for i in range(1, n_cycles + 1):
grid.spin_cycle()
if str(grid) in state_dict:
offset = state_dict[str(grid)]
cycle_length = i - offset
delta = (n_cycles - offset) % cycle_length
res_grid = Grid.from_input(lookup[delta + offset])
return res_grid.total_load
state_dict[str(grid)] = i
lookup.append(str(grid))
return grid.total_load
@main.command()
def entrypoint(path: Path):
"""Entrypoint CLI"""
with open(path, "r") as f:
input = f.read().strip()
logger.info(f"Task 01: {task01(input)}")
logger.info(f"Task 02: {task02(input)}")
if __name__ == "__main__":
main()