Skip to content

Commit

Permalink
Day 14, Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeladler committed Dec 15, 2023
1 parent 3404d75 commit d715f30
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 29 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Compiled using clang 16 and LTO.
| 11 | 980 µs | 1.7 ms |
| 12 | 347 ms | 554 ms |
| 13 | 248 µs | 486 µs |
| 14 | | 53.4 ms |

## 🙏 Acknowledgments and Resources

Expand Down
36 changes: 20 additions & 16 deletions puzzle/day14.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

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.
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.
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:
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:

O....#....
O.OO#....#
Expand Down Expand Up @@ -40,8 +40,8 @@ O..#.OO...

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:
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:

OOOO.#.O.. 10
OO..#....# 9
Expand All @@ -60,14 +60,13 @@ Tilt the platform so that the rounded rocks all roll north. Afterward, what is t

Your puzzle answer was 106997.

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

--- Part Two ---

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!
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.
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:

Expand Down Expand Up @@ -107,15 +106,20 @@ After 3 cycles:
#...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.
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?

Answer:
Your puzzle answer was 99641.

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.

130 changes: 119 additions & 11 deletions src/day14/solve.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@
#include "solve.h"
#include "aoc/all.h"

#include <inttypes.h>
#include <xxhash.h>

#define MAX_DIM 100
#define ROLLING 'O'
#define FIXED_POINT '#'
#define EMPTY '.'

#define MAX_CYCLE_LEN 512

#define P
#define T XXH128_hash_t
#include <ust.h>

static char platform[MAX_DIM][MAX_DIM];

static void tilt_north(int rows, int cols) {
Expand All @@ -22,16 +31,64 @@ static void tilt_north(int rows, int cols) {
char c = platform[y][x];
if (c != ROLLING) continue;
int new_y = y;
while (new_y >= 1 && platform[new_y - 1][x] == EMPTY) { new_y--; }
while (new_y - 1 >= 0 && platform[new_y - 1][x] == EMPTY) { new_y--; }
if (new_y != y) {
// log_debug("row %d, col %d moves to row %d", y, x, new_y);
platform[new_y][x] = c;
platform[y][x] = EMPTY;
}
}
}
}

static void tilt_south(int rows, int cols) {
for (int y = rows - 2; y >= 0; y--) {
for (int x = 0; x < cols; x++) {
char c = platform[y][x];
if (c != ROLLING) continue;
int new_y = y;
while (new_y + 1 < rows && platform[new_y + 1][x] == EMPTY) { new_y++; }
if (new_y != y) {
log_debug("row %d, col %d moves to row %d", y, x, new_y);
// log_debug("row %d, col %d moves to row %d", y, x, new_y);
platform[new_y][x] = c;
platform[y][x] = EMPTY;
}
}
}
}

static void tilt_east(int rows, int cols) {
for (int x = cols - 2; x >= 0; x--) {
for (int y = 0; y < rows; y++) {
char c = platform[y][x];
if (c != ROLLING) continue;
int new_x = x;
while (new_x + 1 < cols && platform[y][new_x + 1] == EMPTY) { new_x++; }
if (new_x != x) {
// log_debug("row %d, col %d moves to col %d", y, x, new_x);
platform[y][new_x] = c;
platform[y][x] = EMPTY;
}
}
}
}

static void tilt_west(int rows, int cols) {
for (int x = 1; x < cols; x++) {
for (int y = 0; y < rows; y++) {
char c = platform[y][x];
if (c != ROLLING) continue;
int new_x = x;
while (new_x - 1 >= 0 && platform[y][new_x - 1] == EMPTY) { new_x--; }
if (new_x != x) {
// log_debug("row %d, col %d moves to col %d", y, x, new_x);
platform[y][new_x] = c;
platform[y][x] = EMPTY;
}
}
}
}

static i64 compute_total_load(int rows, int cols) {
i64 result = 0;
for (int y = 0; y < rows; y++) {
Expand All @@ -43,16 +100,19 @@ static i64 compute_total_load(int rows, int cols) {
return result;
}

static void print_platform(int rows, int cols) {
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) { printf("%c", platform[y][x]); }
puts("");
}
static size_t hash(XXH128_hash_t *x) {
const size_t prime = 31;
size_t hash = 17;
hash = hash * prime + (size_t)x->low64;
hash = hash * prime + (size_t)x->high64;
return hash;
}

static int equal(XXH128_hash_t *a, XXH128_hash_t *b) { return a->low64 == b->low64 && a->high64 == b->high64; }

void solve(const char *buf, size_t buf_size, Solution *result) {
size_t pos = 0;
int rows, cols;
int rows, cols = 0;
{ // parser
int y = 0, x = 0;
while (pos < buf_size) {
Expand All @@ -68,10 +128,58 @@ void solve(const char *buf, size_t buf_size, Solution *result) {
rows = y;
}

tilt_north(rows, cols);

i64 part1, part2 = 0;
part1 = compute_total_load(rows, cols);

XXH128_hash_t hashes[MAX_CYCLE_LEN];
int hashes_count = 0;
_cleanup_(ust_XXH128_hash_t_free) ust_XXH128_hash_t seen = ust_XXH128_hash_t_init(hash, equal);
ust_XXH128_hash_t_reserve(&seen, MAX_CYCLE_LEN);

XXH128_hash_t duplicate;

// find cycle
int cycle = 0;
for (; cycle < MAX_CYCLE_LEN; cycle++) {
tilt_north(rows, cols);
if (cycle == 0) { part1 = compute_total_load(rows, cols); }

tilt_west(rows, cols);
tilt_south(rows, cols);
tilt_east(rows, cols);

XXH128_hash_t value = XXH3_128bits(&platform[0][0], rows * cols);
hashes[hashes_count++] = value;

ust_XXH128_hash_t_node *node = ust_XXH128_hash_t_find(&seen, value);
if (node != NULL) {
duplicate = value;
break;
} else {
ust_XXH128_hash_t_insert(&seen, value);
}
}

int cycle_start = 0;
while (!equal(&hashes[cycle_start], &duplicate)) { cycle_start++; }
int cycle_len = 1;
while (!equal(&hashes[cycle_start + cycle_len], &duplicate)) { cycle_len++; }
log_debug("cycle starts at %d and has length %d", cycle_start, cycle_len);
// we are at the begining of the cycle
log_debug("we are at cycle %d", cycle);

int last_cycle = 1000000000 - 1;

int delta = last_cycle - cycle;
log_debug("%d cycles left to compute", delta);

int rem = last_cycle % delta; // shortcut
for (int i = 0; i <= rem; i++) {
tilt_north(rows, cols);
tilt_west(rows, cols);
tilt_south(rows, cols);
tilt_east(rows, cols);
}
part2 = compute_total_load(rows, cols);

snprintf(result->part1, sizeof(result->part1), "%ld", part1);
snprintf(result->part2, sizeof(result->part2), "%ld", part2);
Expand Down
4 changes: 2 additions & 2 deletions src/day14/solve_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ O.#..O.#.#\n\
Solution solution;
solve(buf, strlen(buf), &solution);
ASSERT_STR("136", solution.part1);
// ASSERT_STR("0", solution.part2);
// ASSERT_STR("64", solution.part2);
}

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

Expand Down

0 comments on commit d715f30

Please sign in to comment.