Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeladler committed Dec 15, 2023
1 parent 412e28b commit 3a85d17
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions src/day14/solve.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
#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 Down Expand Up @@ -101,6 +107,16 @@ static void print_platform(int rows, int cols) {
}
}

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;
Expand All @@ -123,17 +139,37 @@ void solve(const char *buf, size_t buf_size, Solution *result) {

int total_cycles = 1000000000;

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 duplicates[MAX_CYCLE_LEN];
int duplicates_count = 0;

// find cycle
for (int cycle = 1; cycle <= 200; cycle++) {
for (int cycle = 1; cycle <= MAX_CYCLE_LEN; 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);

XXH128_hash_t hash = XXH3_128bits(&platform[0][0], rows * cols);
printf("%d: %lu%lu\n", cycle, hash.low64, hash.high64);
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) {
log_debug("cycle %d seen before", cycle);
duplicates[duplicates_count++] = value;
} else {
ust_XXH128_hash_t_insert(&seen, value);
}
}

for (int i = 0; i < duplicates_count; i++) {
// printf("duplicate %d: %lu%lu\n", i, duplicates[i].low64, duplicates[i].high64);
}

// print_platform(rows, cols);
Expand Down

0 comments on commit 3a85d17

Please sign in to comment.