Skip to content

Commit

Permalink
Day 16, Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeladler committed Dec 16, 2023
1 parent ced2f9b commit 112af80
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 43 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Compiled using clang 16 and LTO.
| 13 | 248 µs | 486 µs |
| 14 | | 53.4 ms |
| 15 | | 1 ms |
| 16 | | 33.5 ms |

## 🙏 Acknowledgments and Resources

Expand Down
62 changes: 25 additions & 37 deletions puzzle/day16.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@

--- Day 16: The Floor Will Be Lava ---

With the beam of light completely focused somewhere, the reindeer leads you deeper still into the Lava Production Facility. At some
point, you realize that the steel facility walls have been replaced with cave, and the doorways are just cave, and the floor is cave,
and you're pretty sure this is actually just a giant cave.
With the beam of light completely focused somewhere, the reindeer leads you deeper still into the Lava Production Facility. At some point, you realize that the steel facility walls have been replaced with cave, and the doorways are just cave, and the floor is cave, and
you're pretty sure this is actually just a giant cave.

Finally, as you approach what must be the heart of the mountain, you see a bright light in a cavern up ahead. There, you discover that
the beam of light you so carefully focused is emerging from the cavern wall closest to the facility and pouring all of its energy into a
Finally, as you approach what must be the heart of the mountain, you see a bright light in a cavern up ahead. There, you discover that the beam of light you so carefully focused is emerging from the cavern wall closest to the facility and pouring all of its energy into a
contraption on the opposite side.

Upon closer inspection, the contraption appears to be a flat, two-dimensional square grid containing empty space (.), mirrors (/ and \),
and splitters (| and -).
Upon closer inspection, the contraption appears to be a flat, two-dimensional square grid containing empty space (.), mirrors (/ and \), and splitters (| and -).

The contraption is aligned so that most of the beam bounces around the grid, but each tile on the grid converts some of the beam's light
into heat to melt the rock in the cavern.
The contraption is aligned so that most of the beam bounces around the grid, but each tile on the grid converts some of the beam's light into heat to melt the rock in the cavern.

You note the layout of the contraption (your puzzle input). For example:

Expand All @@ -28,21 +24,16 @@ You note the layout of the contraption (your puzzle input). For example:
.|....-|.\
..//.|....

The beam enters in the top-left corner from the left and heading to the right. Then, its behavior depends on what it encounters as it
moves:
The beam enters in the top-left corner from the left and heading to the right. Then, its behavior depends on what it encounters as it moves:

If the beam encounters empty space (.), it continues in the same direction.
If the beam encounters a mirror (/ or \), the beam is reflected 90 degrees depending on the angle of the mirror. For instance, a
rightward-moving beam that encounters a / mirror would continue upward in the mirror's column, while a rightward-moving beam that
encounters a \ mirror would continue downward from the mirror's column.
If the beam encounters the pointy end of a splitter (| or -), the beam passes through the splitter as if the splitter were empty space.
For instance, a rightward-moving beam that encounters a - splitter would continue in the same direction.
If the beam encounters the flat side of a splitter (| or -), the beam is split into two beams going in each of the two directions the
splitter's pointy ends are pointing. For instance, a rightward-moving beam that encounters a | splitter would split into two beams: one
that continues upward from the splitter's column and one that continues downward from the splitter's column.

Beams do not interact with other beams; a tile can have many beams passing through it at the same time. A tile is energized if that tile
has at least one beam pass through it, reflect in it, or split in it.
If the beam encounters a mirror (/ or \), the beam is reflected 90 degrees depending on the angle of the mirror. For instance, a rightward-moving beam that encounters a / mirror would continue upward in the mirror's column, while a rightward-moving beam that encounters a \
mirror would continue downward from the mirror's column.
If the beam encounters the pointy end of a splitter (| or -), the beam passes through the splitter as if the splitter were empty space. For instance, a rightward-moving beam that encounters a - splitter would continue in the same direction.
If the beam encounters the flat side of a splitter (| or -), the beam is split into two beams going in each of the two directions the splitter's pointy ends are pointing. For instance, a rightward-moving beam that encounters a | splitter would split into two beams: one that
continues upward from the splitter's column and one that continues downward from the splitter's column.

Beams do not interact with other beams; a tile can have many beams passing through it at the same time. A tile is energized if that tile has at least one beam pass through it, reflect in it, or split in it.

In the above example, here is how the beam of light bounces around the contraption:

Expand All @@ -57,8 +48,7 @@ In the above example, here is how the beam of light bounces around the contrapti
.|<<<2-|.\
.v//.|.v..

Beams are only shown on empty tiles; arrows indicate the direction of the beams. If a tile contains beams moving in multiple directions,
the number of distinct directions is shown instead. Here is the same diagram but instead only showing whether a tile is energized (#) or
Beams are only shown on empty tiles; arrows indicate the direction of the beams. If a tile contains beams moving in multiple directions, the number of distinct directions is shown instead. Here is the same diagram but instead only showing whether a tile is energized (#) or
not (.):

######....
Expand All @@ -74,23 +64,17 @@ not (.):

Ultimately, in this example, 46 tiles become energized.

The light isn't energizing enough tiles to produce lava; to debug the contraption, you need to start by analyzing the current situation.
With the beam starting in the top-left heading right, how many tiles end up being energized?
The light isn't energizing enough tiles to produce lava; to debug the contraption, you need to start by analyzing the current situation. With the beam starting in the top-left heading right, how many tiles end up being energized?

Your puzzle answer was 7632.

The first half of this puzzle is complete! It provides one gold star: *

--- Part Two ---

As you try to work out what might be wrong, the reindeer tugs on your shirt and leads you to a nearby control panel. There, a collection
of buttons lets you align the contraption so that the beam enters from any edge tile and heading away from that edge. (You can choose
either of two directions for the beam if it starts on a corner; for instance, if the beam starts in the bottom-right corner, it can
start heading either left or upward.)
As you try to work out what might be wrong, the reindeer tugs on your shirt and leads you to a nearby control panel. There, a collection of buttons lets you align the contraption so that the beam enters from any edge tile and heading away from that edge. (You can choose
either of two directions for the beam if it starts on a corner; for instance, if the beam starts in the bottom-right corner, it can start heading either left or upward.)

So, the beam could start on any tile in the top row (heading downward), any tile in the bottom row (heading upward), any tile in the
leftmost column (heading right), or any tile in the rightmost column (heading left). To produce lava, you need to find the configuration
that energizes as many tiles as possible.
So, the beam could start on any tile in the top row (heading downward), any tile in the bottom row (heading upward), any tile in the leftmost column (heading right), or any tile in the rightmost column (heading left). To produce lava, you need to find the configuration that
energizes as many tiles as possible.

In the above example, this can be achieved by starting the beam in the fourth tile from the left in the top row:

Expand Down Expand Up @@ -120,9 +104,13 @@ Using this configuration, 51 tiles are energized:

Find the initial beam configuration that energizes the largest number of tiles; how many tiles are energized in that configuration?

Answer:
Your puzzle answer was 8023.

Both parts of this puzzle are complete! They provide two gold stars: **

At this point, you should return to your Advent calendar and try another puzzle.

Although it hasn't changed, you can still get your puzzle input.
If you still want to see it, you can get your puzzle input.

You can also [Shareon Twitter Mastodon] this puzzle.

41 changes: 37 additions & 4 deletions src/day16/solve.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,43 @@ void solve(char *buf, size_t buf_size, Solution *result) {
}
log_debug("rows: %d, cols: %d", rows, cols);

beam_t start = (beam_t){.x = 0, .y = 0, .direction = EAST};
int part1 = run_simulation(start, rows, cols);

int part2 = 0;
// brute force
int part1 = 0, part2 = 0;
for (direction_t direction = 0; direction < 4; direction++) {
// top row
if (direction != NORTH) {
for (int x = 0; x < cols; x++) {
beam_t start = (beam_t){.x = x, .y = 0, .direction = direction};
int value = run_simulation(start, rows, cols);
if (direction == EAST && x == 0) part1 = value;
part2 = MAX(part2, value);
}
}
// bottom row
if (direction != SOUTH) {
for (int x = 0; x < cols; x++) {
beam_t start = (beam_t){.x = x, .y = rows - 1, .direction = direction};
int value = run_simulation(start, rows, cols);
part2 = MAX(part2, value);
}
}
// left row
if (direction != WEST) {
for (int y = 0; y < rows; y++) {
beam_t start = (beam_t){.x = 0, .y = y, .direction = direction};
int value = run_simulation(start, rows, cols);
part2 = MAX(part2, value);
}
}
// right row
if (direction != EAST) {
for (int y = 0; y < rows; y++) {
beam_t start = (beam_t){.x = cols - 1, .y = y, .direction = direction};
int value = run_simulation(start, rows, cols);
part2 = MAX(part2, value);
}
}
}

aoc_itoa(part1, result->part1, 10);
aoc_itoa(part2, result->part2, 10);
Expand Down
4 changes: 2 additions & 2 deletions src/day16/solve_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ CTEST(day16, example) {
Solution solution;
solve(buf, strlen(buf), &solution);
ASSERT_STR("46", solution.part1);
// ASSERT_STR("0", solution.part2);
ASSERT_STR("51", solution.part2);
}

#ifdef HAVE_INPUTS
CTEST(day16, real) {
Solution solution;
solve_input("input/" DAY ".txt", &solution);
ASSERT_STR("7632", solution.part1);
// ASSERT_STR("0", solution.part2);
ASSERT_STR("8023", solution.part2);
}
#endif

Expand Down

0 comments on commit 112af80

Please sign in to comment.