-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday17.py
More file actions
165 lines (141 loc) · 4.58 KB
/
Copy pathday17.py
File metadata and controls
165 lines (141 loc) · 4.58 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
from dataclasses import dataclass
import re
import functools
@dataclass(frozen=True)
class Coords:
x: int
y: int
@dataclass(frozen=True)
class Piece:
# relative locations to left-most square that makes up this piece
locs: list[Coords]
height: int
bottom: int = 0
pieces = [
Piece(locs=[Coords(0, 0), Coords(1, 0), Coords(2, 0), Coords(3, 0)], height=1),
Piece(
locs=[Coords(0, 0), Coords(1, 1), Coords(1, 0), Coords(1, -1), Coords(2, 0)],
bottom=-1,
height=2,
),
Piece(
locs=[Coords(0, 0), Coords(1, 0), Coords(2, 0), Coords(2, 1), Coords(2, 2)],
height=3,
),
Piece(locs=[Coords(0, 0), Coords(0, 1), Coords(0, 2), Coords(0, 3)], height=4),
Piece(locs=[Coords(0, 0), Coords(0, 1), Coords(1, 0), Coords(1, 1)], height=2),
]
def collides(loc: Coords, piece: Piece, grid: dict[Coords, bool]) -> bool:
for rel in piece.locs:
test_loc = Coords(x=loc.x + rel.x, y=loc.y + rel.y)
if test_loc in grid:
return True
if test_loc.x < 0 or test_loc.x >= 7:
return True
if test_loc.y < 0:
return True
return False
def drop_piece(
num: int,
grid: dict[Coords, bool],
height: int,
instr_offset: int,
instructions: str,
) -> tuple[int, int]:
piece = pieces[num % len(pieces)]
piece_loc = Coords(x=2, y=height + 3 - piece.bottom)
idx = instr_offset
iterations = 0
while True:
# blow the piece left or right
if instructions[idx] == "<":
new_loc = Coords(x=piece_loc.x - 1, y=piece_loc.y)
elif instructions[idx] == ">":
new_loc = Coords(x=piece_loc.x + 1, y=piece_loc.y)
if not collides(new_loc, piece, grid):
piece_loc = new_loc
idx += 1
if idx >= len(instructions):
idx = 0
iterations += 1
# drop the piece one position
new_loc = Coords(x=piece_loc.x, y=piece_loc.y - 1)
if collides(new_loc, piece, grid):
break
piece_loc = new_loc
# fill in the grid with this piece
new_height = height
for rel in piece.locs:
fill_loc = Coords(x=piece_loc.x + rel.x, y=piece_loc.y + rel.y)
assert fill_loc not in grid
grid[fill_loc] = True
new_height = max(fill_loc.y + 1, new_height)
return iterations, new_height
def print_tower_top(height: int, grid: dict[Coords, bool]):
for ydiff in range(1, 20):
if height - ydiff < 0:
break
for x in range(7):
coord = Coords(x, height - ydiff)
if coord in grid:
print("#", end="")
else:
print(".", end="")
print()
def top_of_grid_as_string(grid: dict[Coords, bool], height: int, rows: int) -> str:
top = max(height, rows)
grid_str = ""
for row in range(rows):
yval = top - rows - 1
for x in range(7):
coords = Coords(x, yval)
grid_str = grid_str + f"{'#' if coords in grid else '.'}"
return grid_str
@dataclass(frozen=True)
class GridHash:
top_of_grid: str
piece_id: int
instruction_id: int
with open("day17.txt", "r") as file:
grid = {}
instructions = file.readline().strip()
idx = 0
iteration = 0
height = 0
while iteration < 2022:
instructions_used, height = drop_piece(
iteration, grid, height, idx, instructions
)
idx += instructions_used
if idx >= len(instructions):
idx %= len(instructions)
iteration += 1
print(height)
grid = {}
cache: dict[GridHash, tuple[int, int]] = {}
loop_height = 0
iteration = 0
height = 0
idx = 0
TARGET_ITERATIONS = 1000000000000
shortcut = False
while iteration < TARGET_ITERATIONS:
grid_str = top_of_grid_as_string(grid, height, 20)
grid_hash = GridHash(grid_str, iteration % len(pieces), idx)
if grid_hash in cache and not shortcut:
orig_iteration, orig_height = cache[grid_hash]
loop_length = iteration - orig_iteration
num_loops = (TARGET_ITERATIONS - iteration) // loop_length
loop_height = num_loops * (height - orig_height)
iteration += num_loops * loop_length
shortcut = True
else:
cache[grid_hash] = (iteration, height)
instructions_used, height = drop_piece(
iteration, grid, height, idx, instructions
)
idx += instructions_used
if idx >= len(instructions):
idx %= len(instructions)
iteration += 1
print(height + loop_height)