From 3a85d17f873259788fd17fd00ff4916df244e43b Mon Sep 17 00:00:00 2001 From: Michael Adler Date: Fri, 15 Dec 2023 21:51:54 +0100 Subject: [PATCH] wip --- src/day14/solve.c | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/src/day14/solve.c b/src/day14/solve.c index 33b7b9a..47119cf 100644 --- a/src/day14/solve.c +++ b/src/day14/solve.c @@ -17,6 +17,12 @@ #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) { @@ -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; @@ -123,8 +139,16 @@ 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); } @@ -132,8 +156,20 @@ void solve(const char *buf, size_t buf_size, Solution *result) { 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);