Metadata
-
Date
-
Tagged
-
Part of series
- Advent of Code 2023 Day 1
- Advent of Code 2023 Day 2
- Advent of Code 2023 Day 3
- Advent of Code 2023 Day 4
- Advent of Code 2023 Day 5
- Advent of Code 2023 Day 6
- Advent of Code 2023 Day 7
- Advent of Code 2023 Day 8
- Advent of Code 2023 Day 9
- Advent of Code 2023 Day 10
- Advent of Code 2023 Day 11
- Advent of Code 2023 Day 12
- Advent of Code 2023 Day 13
- Advent of Code 2023 Day 14
- Advent of Code 2023 Day 15
- Advent of Code 2023 Day 16
- Advent of Code 2023 Day 17
- Advent of Code 2023 Day 18
- Advent of Code 2023 Day 19
- Advent of Code 2023 Day 20
- Advent of Code 2023 Day 21
- Advent of Code 2023 Day 22
- Advent of Code 2023 Day 23
- Advent of Code 2023 Day 25
-
Older post
-
Newer post
Advent of Code 2023 Day 14
Day 14: Parabolic Reflector Dish
https://adventofcode.com/2023/day/14
Turns out the mirrors from yesterday are used to power a concentrated solar mirror thingamajig. The mirrors are not calibrated correctly, that must be why the lava isn’t being made.
It being an advent of code elven mirror, it’s calibrated in the most convoluted way possible.
It uses a bunch of rocks to put pressure on the mirrors.
Today’s input is the arrangement of the calibration device (so, plane with a bunch of rocks on it).
An example input looks like this:
O
is a round rock#
is a square rock.
is empty space
You can tilt the calibration device.
The round rocks roll, the square rocks stay put.
Parsing
An enum to keep track of what a tile holds:
A function to parse the input into a 2D list of Tile
:
Part 1
The question asks how much total load would be on the north support beams after sliding the rocks north.
The load is determined by the location of the rounded rocks.
Each rounded rock contributes a load equal to the number of rows from the rock to the south edge of the platform, including the row the rock is on.
Helpers
A function that slides all round rocks to the north:
A function that calculated the total weight of a grid:
Combining these helpers makes the final code for part1 very short.
Code
Part 2
The platform has a “spin cycle” button.
It slides the rocks: north, the west, then south, then east.
If you do that enough times, the mirrors will surely be calibrated.
The question asks what the total load is after 1000000000 cycles.
So, Advent of Code, huge number of cycles, that must mean a cycle detection problem.
First, I’ll code enough to try this problem brute-force, but I’m not getting my hopes up.
Helpers
First, a function that applies a full cycle to a grid (tilting north, west, south, east).
Tilting north, west, south, east means tilting in a counterclockwise pattern.
That’s the same as:
- Tilt north
- Turn grid clockwise
- Tilt north
- Turn grid clockwise
- Tilt north
- Turn grid clockwise
- Tilt north
- Turn grid clockwise
So the next helper is the one that turns a grid clockwise. To do that, change every (x, y) coordinate to an (y, -x) coordinate.
As I suspected, brute-forcing is a no-go. Time to put on the cycle-detecting hat! (It’s a regular hat, my ears are cold.)
I keep track of every grid pattern I saw so far. Each time I cycle the grid, I check if the new pattern exists in that list.
If the pattern appeared before, I hit the start of a new cycle. The length of a cycle is the total length of the list - the index where the repeat happened.
Using that length, figure out the index of the pattern the entire loop will end on.