Skip to content

Commit

Permalink
Day 14, Part 2: WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeladler committed Dec 15, 2023
1 parent 3a80e39 commit 0acedd9
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 6 deletions.
99 changes: 94 additions & 5 deletions src/day14/solve.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,23 @@
#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 '.'

typedef struct {
XXH64_hash_t hash;
int cycle;
} Seen;

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

static char platform[MAX_DIM][MAX_DIM];

static void tilt_north(int rows, int cols) {
Expand All @@ -22,16 +34,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 @@ -50,6 +110,12 @@ static void print_platform(int rows, int cols) {
}
}

static inline XXH64_hash_t hash_platform(int rows, int cols) { return XXH3_64bits(&platform[0][0], rows * cols); }

static size_t hash_helper(Seen *s) { return s->hash; }

static int equal_helper(Seen *lhs, Seen *rhs) { return lhs->hash == rhs->hash; }

void solve(const char *buf, size_t buf_size, Solution *result) {
size_t pos = 0;
int rows, cols;
Expand All @@ -68,10 +134,33 @@ 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);

int total_cycles = 1000000000;
// TODO: find repeating pattern

_cleanup_(ust_Seen_free) ust_Seen seen = ust_Seen_init(hash_helper, equal_helper);

for (int cycle = 1; cycle <= 1000000; cycle++) {
tilt_north(rows, cols);
if (cycle == 1) { part1 = compute_total_load(rows, cols); }

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

if (cycle == 175) print_platform(rows, cols);

Seen s = {.hash = hash_platform(rows, cols), .cycle = cycle};
ust_Seen_node *node = ust_Seen_find(&seen, s);
if (node != NULL) {
printf("cycle %d collides with entry from cycle %d\n", cycle, node->key.cycle);
break;
}
ust_Seen_insert(&seen, s);
}

print_platform(rows, cols);

snprintf(result->part1, sizeof(result->part1), "%ld", part1);
snprintf(result->part2, sizeof(result->part2), "%ld", part2);
Expand Down
2 changes: 1 addition & 1 deletion src/day14/solve_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0acedd9

Please sign in to comment.