Index: aoc2024.c ================================================================== --- aoc2024.c +++ aoc2024.c @@ -4,36 +4,52 @@ #include #include #include #include "aocdailies.h" #include "aocutils.h" + +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; + } + return NULL; +} void aoc202405(char *data, size_t len) { (void)len; // unused argument unsigned pagepair[1200][2], npp = 0; // 1200 works for me char *line = strtok(data, "\n"); while (strchr(line, '|')) { char *err; pagepair[npp][0] = strtoul(line, &err, 10); err += 1; // skip '|' - pagepair[npp][1] = strtoul(line, &err, 10); + pagepair[npp][1] = strtoul(err, &err, 10); npp++; line = strtok(NULL, "\n"); } - unsigned update[32], nupdates, maxupdates = 0; // 32 works dor me - while ((line = strtok(NULL, "\n")) != NULL) { + unsigned update[32], nupdates; // 32 works for me + unsigned accumsum = 0; + do { nupdates = 0; char *err = line; for (;;) { update[nupdates++] = strtoul(err, &err, 10); if (*err == ',') err++; else break; } - for (size_t k = 0; k < nupdates; k++) { - //... + if (nupdates % 2 == 0) printf("even number of updates found!\n"); + unsigned accumulate = update[nupdates / 2]; + 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; } } - } + accumsum += accumulate; + } while ((line = strtok(NULL, "\n")) != NULL); + printf("The sum of middle update numbers is {%u}\n", accumsum); } static size_t rcmap(unsigned size, unsigned row, unsigned col) { return row * (size + 1) + col; }