Day 14: Parabolic Reflector Dish

Started ~1 hour, 34 minutes late.
Completed in ~3 hours, 19 minutes.
6,846ᵗʰ worldwide.
2023-12-14

You reach the place where all of the mirrors were pointing: a massive parabolic reflector dish attached to the side of another large mountain.

The dish is made up of many small mirrors, but while the mirrors themselves are roughly in the shape of a parabolic reflector dish, each individual mirror seems to be pointing in slightly the wrong direction. If the dish is meant to focus light, all it's doing right now is sending it in a vague direction.

This system must be what provides the energy for the lava! If you focus the reflector dish, maybe you can go where it's pointing and use the light to fix the lava production.

Upon closer inspection, the individual mirrors each appear to be connected via an elaborate system of ropes and pulleys to a large metal platform below the dish. The platform is covered in large rocks of various shapes. Depending on their position, the weight of the rocks deforms the platform, and the shape of the platform controls which ropes move and ultimately the focus of the dish.

In short: if you move the rocks, you can focus the dish. The platform even has a control panel on the side that lets you tilt it in one of four directions! The rounded rocks (O) will roll when the platform is tilted, while the cube-shaped rocks (#) will stay in place. You note the positions of all of the empty spaces (.) and rocks (your puzzle input). For example:

1
2
3
4
5
6
7
8
9
10
O....#.... O.OO#....# .....##... OO.#O....O .O.....O#. O.#..O.#.# ..O..#O..O .......O.. #....###.. #OO..#....

Start by tilting the lever so all of the rocks will slide north as far as they will go:

1
2
3
4
5
6
7
8
9
10
OOOO.#.O.. OO..#....# OO..O##..O O..#.OO... ........#. ..#....#.# ..O..#.O.O ..O....... #....###.. #....#....

You notice that the support beams along the north side of the platform are damaged; to ensure the platform doesn't collapse, you should calculate the total load on the north support beams.

The amount of load caused by a single rounded rock (O) is equal to the number of rows from the rock to the south edge of the platform, including the row the rock is on. (Cube-shaped rocks (#) don't contribute to load.) So, the amount of load caused by each rock in each row is as follows:

1
2
3
4
5
6
7
8
9
10
OOOO.#.O.. 10 OO..#....# 9 OO..O##..O 8 O..#.OO... 7 ........#. 6 ..#....#.# 5 ..O..#.O.O 4 ..O....... 3 #....###.. 2 #....#.... 1

The total load is the sum of the load caused by all of the rounded rocks. In this example, the total load is 136.

Tilt the platform so that the rounded rocks all roll north. Afterward, what is the total load on the north support beams?

The tilt_left function does all the heavy lifting. This works because we consider each "line" independent from the others, where a "line" is a column of characters.

Basically, we just try to move round rocks "left" until we hit either another round rock, the edge of the board, or a square rock.

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
from collections import Counter from pathlib import Path import pytest def tilt_left(line: str) -> str: rock_map: Counter[int] = Counter({0: 0}) square_rock_map: list[int] = [-1] current_index = 0 rock_line: list[str] = [] for index, character in enumerate(line): if character == "#": current_index += 1 rock_line.append("#") square_rock_map.append(index) elif character == ".": rock_line.append(".") elif character == "O": rock_map[current_index] += 1 rock_line.append(".") for index, square_rock_index in enumerate(square_rock_map): round_rocks = rock_map[index] for offset in range(round_rocks): rock_line[square_rock_index + offset + 1] = "O" return "".join(rock_line) def runner(document: list[str]) -> int: total = 0 line_size = len(document[0]) lines: list[str] = ["" for _ in range(line_size)] for line in document: for index, character in enumerate(line): lines[index] += character for index, line in enumerate(lines): lines[index] = tilt_left(line) for row_index in range(line_size): line_total = 0 for line in lines: if line[row_index] == "O": line_total += 1 line_total *= line_size - row_index total += line_total return total @pytest.mark.parametrize( "line,output", [ ("OOO", "OOO"), ("O.O", "OO."), ("O..O", "OO.."), ("O#.O", "O#O."), ("O.#.O", "O.#O."), ("O.O#..OO.#.O", "OO.#OO...#O."), ], ) def test_tilt_left(line: str, output: str) -> None: assert tilt_left(line) == output @pytest.mark.parametrize( "filename,output", [ ("example-1.txt", 136), ("example-2.txt", 110677), ], ) def test_runner(filename: str, output: int) -> None: with open(Path(__file__).with_name(filename)) as file: result = runner(file.read().splitlines()) assert result == output

Answer: 110,677

The parabolic reflector dish deforms, but not in a way that focuses the beam. To do that, you'll need to move the rocks to the edges of the platform. Fortunately, a button on the side of the control panel labeled "spin cycle" attempts to do just that!

Each cycle tilts the platform four times so that the rounded rocks roll north, then west, then south, then east. After each tilt, the rounded rocks roll as far as they can before the platform tilts in the next direction. After one cycle, the platform will have finished rolling the rounded rocks in those four directions in that order.

Here's what happens in the example above after each of the first few cycles:

After 1 cycle:

1
2
3
4
5
6
7
8
9
10
.....#.... ....#...O# ...OO##... .OO#...... .....OOO#. .O#...O#.# ....O#.... ......OOOO #...O###.. #..OO#....

After 2 cycles:

1
2
3
4
5
6
7
8
9
10
.....#.... ....#...O# .....##... ..O#...... .....OOO#. .O#...O#.# ....O#...O .......OOO #..OO###.. #.OOO#...O

After 3 cycles:

1
2
3
4
5
6
7
8
9
10
.....#.... ....#...O# .....##... ..O#...... .....OOO#. .O#...O#.# ....O#...O .......OOO #...O###.O #.OOO#...O

This process should work if you leave it running long enough, but you're still worried about the north support beams. To make sure they'll survive for a while, you need to calculate the total load on the north support beams after 1000000000 cycles.

In the above example, after 1000000000 cycles, the total load on the north support beams is 64.

Run the spin cycle for 1000000000 cycles. Afterward, what is the total load on the north support beams?

Similar to Part 1, with some additional functions for rotating the board.

The key insight here is that the board will repeat after N cycles of rotating and tilting. Once we reach a repeat board configuration, we count the number of steps in a cycle and can figure out the final position from there.

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
from collections import Counter from pathlib import Path import pytest def tilt_left(line: str) -> str: rock_map: Counter[int] = Counter({0: 0}) square_rock_map: list[int] = [-1] current_index = 0 rock_line: list[str] = [] for index, character in enumerate(line): if character == "#": current_index += 1 rock_line.append("#") square_rock_map.append(index) elif character == ".": rock_line.append(".") elif character == "O": rock_map[current_index] += 1 rock_line.append(".") for index, square_rock_index in enumerate(square_rock_map): round_rocks = rock_map[index] for offset in range(round_rocks): rock_line[square_rock_index + offset + 1] = "O" return "".join(rock_line) def rotate_90(raw_lines: list[str]) -> list[str]: # Rotates 90 degrees clockwise line_size = len(raw_lines[0]) lines: list[str] = ["" for _ in range(line_size)] for line in raw_lines: for index, character in enumerate(reversed(line)): lines[index] += character return lines def rotate_180(raw_lines: list[str]) -> list[str]: new_lines = raw_lines.copy() for _ in range(2): new_lines = rotate_90(new_lines) return new_lines def rotate_270(raw_lines: list[str]) -> list[str]: new_lines = raw_lines.copy() for _ in range(3): new_lines = rotate_90(new_lines) return new_lines def runner(document: list[str]) -> int: size = 1_000_000_000 counter = 0 cycle_repeats: int | None = None cycle_counter: dict[str, int] = {} cycles: dict[int, list[str]] = {} while counter < size: north = [tilt_left(line) for line in rotate_90(document)] west = [tilt_left(line) for line in rotate_270(north)] south = [tilt_left(line) for line in rotate_270(west)] east = [tilt_left(line) for line in rotate_270(south)] cycled = rotate_180(east) cycles[counter] = cycled cycle_key = "".join(cycled) if cycle_key in cycle_counter: cycle_repeats = cycle_counter[cycle_key] break else: cycle_counter[cycle_key] = counter counter += 1 document = cycled if cycle_repeats is None: last_cycle = cycles[counter - 1] else: final_cycle = ( cycle_repeats + (size - cycle_repeats) % (counter - cycle_repeats) - 1 ) last_cycle = cycles[final_cycle] total = 0 for index, line in enumerate(last_cycle): total += line.count("O") * (len(last_cycle) - index) return total @pytest.mark.parametrize( "line,output", [ ("OOO", "OOO"), ("O.O", "OO."), ("O..O", "OO.."), ("O#.O", "O#O."), ("O.#.O", "O.#O."), ("O.O#..OO.#.O", "OO.#OO...#O."), ], ) def test_tilt_left(line: str, output: str) -> None: assert tilt_left(line) == output @pytest.mark.parametrize( "lines,output", [ (["ABC", "DEF", "GHI"], ["CFI", "BEH", "ADG"]), (["CFI", "BEH", "ADG"], ["IHG", "FED", "CBA"]), (["IHG", "FED", "CBA"], ["GDA", "HEB", "IFC"]), (["GDA", "HEB", "IFC"], ["ABC", "DEF", "GHI"]), ], ) def test_rotate_90(lines: list[str], output: list[str]) -> None: assert rotate_90(lines) == output @pytest.mark.parametrize( "filename,output", [ ("example-1.txt", 64), ("example-2.txt", 90551), ], ) def test_runner(filename: str, output: int) -> None: with open(Path(__file__).with_name(filename)) as file: result = runner(file.read().splitlines()) assert result == output

Answer: 90,551

DayPart 1 TimePart 1 RankPart 2 TimePart 2 Rank
1402:04:229,33203:18:556,846

I started ~1 hour 34 minutes late 🙃.

I ended up making some tests for the rotate functions, that took a bit of fiddling to get correct. And the end result is still very strange looking. I perform a 90 degree roatation, then three 270 degree rotations, then a final 180 degree rotation to complete a cycle. It works, just very strange looking.

Detecting cycles wasn't particularly hard, I could just convert the board to a string and check against a dict/set of strings. I used a dictionary, mapping string-boards to steps, which made some of the math easier.

Once we find our cycle, we just account for the cycle start offset and we can find the final board configuration with a simple lookup table.