Index: aoc2024.c ================================================================== --- aoc2024.c +++ aoc2024.c @@ -33,20 +33,24 @@ if (ndisk == rdisk) { // assume it works rdisk = (13*rdisk)/8; disk = realloc(disk, rdisk * sizeof *disk); } - disk[ndisk++] = -1; + disk[ndisk++] = -1 * (*data - '0'); } data++; } } + // copy disk + int *diskcopy = malloc(ndisk * sizeof *disk); + memcpy(diskcopy, disk, ndisk * sizeof *disk); + int *left = disk, *right = disk + ndisk; for (;;) { - while (*left != -1) left++; + while (*left >= 0) left++; if (left >= right) break; - while (right[-1] == -1) right--; + while (right[-1] < 0) right--; // swap *left and right[-1] int tmp = *--right; *right = *left; *left++ = tmp; } @@ -56,10 +60,13 @@ chksum += (size_t)disk[index] * index; index++; } printf("%llu\n", chksum); free(disk); + + // part 2 + free(diskcopy); } /* === aoc202408 ======================================================= Oh! another square of text! Idea: for all points p with an antenna find all points q>p with