From 1162a565cda10d87e6281f7cba2c33024342d485 Mon Sep 17 00:00:00 2001 From: Michael Adler Date: Fri, 15 Dec 2023 16:46:52 +0100 Subject: [PATCH] Day 14, Part 2 --- puzzle/day14.md | 36 +++++++----- src/day14/solve.c | 130 +++++++++++++++++++++++++++++++++++++---- src/day14/solve_test.c | 4 +- 3 files changed, 141 insertions(+), 29 deletions(-) diff --git a/puzzle/day14.md b/puzzle/day14.md index ccc2f52..2f45962 100644 --- a/puzzle/day14.md +++ b/puzzle/day14.md @@ -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#....# @@ -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 @@ -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: @@ -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. diff --git a/src/day14/solve.c b/src/day14/solve.c index 4899242..6cfec51 100644 --- a/src/day14/solve.c +++ b/src/day14/solve.c @@ -9,11 +9,20 @@ #include "solve.h" #include "aoc/all.h" +#include +#include + #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 + static char platform[MAX_DIM][MAX_DIM]; static void tilt_north(int rows, int cols) { @@ -22,9 +31,25 @@ 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; } @@ -32,6 +57,38 @@ static void tilt_north(int rows, int cols) { } } +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++) { @@ -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) { @@ -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); diff --git a/src/day14/solve_test.c b/src/day14/solve_test.c index ba0dc67..ba7a5cf 100644 --- a/src/day14/solve_test.c +++ b/src/day14/solve_test.c @@ -25,7 +25,7 @@ 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 @@ -33,7 +33,7 @@ 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