Index: aoc2024.c ================================================================== --- aoc2024.c +++ aoc2024.c @@ -4,10 +4,13 @@ #include #include #include #include "aocdailies.h" #include "aocutils.h" + +static void reorder(unsigned *a, size_t na, unsigned (*r)[2], size_t nr) { +} 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; } @@ -35,21 +38,28 @@ update[nupdates++] = strtoul(err, &err, 10); if (*err == ',') err++; else break; } if (nupdates % 2 == 0) printf("even number of updates found!\n"); - unsigned accumulate = update[nupdates / 2]; + unsigned accum = update[nupdates / 2], accumsum2 = 0; for (size_t k = 0; k < npp; k++) { const unsigned *before = vfind(pagepair[k][0], update, nupdates); if (!before) continue; const unsigned *after = vfind(pagepair[k][1], update, nupdates); if (!after) continue; - if (before > after) { accumulate = 0; break; } + if (before > after) { + accum = 0; + // reorder `update` according to `pagepair` + reorder(update, nupdates, pagepair, npp); + accumsum2 += update[nupdates / 2]; + break; + } } - accumsum += accumulate; + 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); } static size_t rcmap(unsigned size, unsigned row, unsigned col) { return row * (size + 1) + col; }