Index: aoc2024.c ================================================================== --- aoc2024.c +++ aoc2024.c @@ -5,15 +5,29 @@ #include #include #include "aocdailies.h" #include "aocutils.h" +static int ppcmp(unsigned a, unsigned b, unsigned (*r)[2], size_t nr) { + for (size_t k = 0; k < nr; k++) { + if ((a == r[k][0]) && (b == r[k][1])) return -1; + if ((a == r[k][1]) && (b == r[k][0])) return 1; + } + return 0; +} + +// can't be bothered to do anything other than bubble sort static void reorder(unsigned *a, size_t na, unsigned (*r)[2], size_t nr) { - (void)a; - (void)na; - (void)r; - (void)nr; + for (size_t range = na; --range > 0; /*void*/) { + for (size_t outer = 0; outer < range; outer++) { + if (ppcmp(a[outer], a[outer+1], r, nr) > 0) { + unsigned tmp = a[outer]; + a[outer] = a[outer+1]; + a[outer+1] = tmp; + } + } + } } static const unsigned *vfind(unsigned v, const unsigned *a, size_t n) { for (size_t k = 0; k < n; k++) { if (a[k] == v) return a + k; @@ -22,27 +36,19 @@ } void aoc202405(char *data, size_t len) { (void)len; // unused argument unsigned pagepair[1200][2], npp = 0; // 1200 works for me - unsigned ppcount[100][2] = {0}; // assume all page numbers < 100 char *line = strtok(data, "\n"); while (strchr(line, '|')) { char *err; pagepair[npp][0] = strtoul(line, &err, 10); - ppcount[pagepair[npp][0]][0]++; err += 1; // skip '|' pagepair[npp][1] = strtoul(err, &err, 10); - ppcount[pagepair[npp][1]][1]++; npp++; line = strtok(NULL, "\n"); } - for (unsigned k = 0; k < 100; k++) { - if (ppcount[k][0] + ppcount[k][1]) - printf("%u: [%u, %u] ", k, ppcount[k][0], ppcount[k][1]); - } - printf("\n"); unsigned update[32], nupdates; // 32 works for me unsigned accumsum = 0, accumsum2 = 0; do { nupdates = 0; char *err = line; @@ -66,12 +72,12 @@ break; } } accumsum += accum; } while ((line = strtok(NULL, "\n")) != NULL); - printf("The sum of middle update numbers is {%u}\n", accumsum); - printf("The sum of middle update numbers for newly ordered updates is {%u}\n", accumsum2); + printf("The sum of middle update numbers is {%u}.\n", accumsum); + printf("The sum of middle update numbers for newly ordered updates is {%u}.\n", accumsum2); } static size_t rcmap(unsigned size, unsigned row, unsigned col) { return row * (size + 1) + col; }