Index: aoc2024.c ================================================================== --- aoc2024.c +++ aoc2024.c @@ -29,10 +29,12 @@ tg.data = data; tg.cols = strchr(data, '\n') - data + 1; tg.rows = tg.cols - 1; // rows includes the '\n' unsigned anti[512][2]; // locations of antinodes; col at index 0, row at 1 unsigned nanti = 0; + unsigned anti2[1024][2]; // locations of antinodes; col at index 0, row at 1 + unsigned nanti2 = 0; char *p = data; while (*p) { while ((*p == '.') || (*p == '\n')) p++; if (*p) { @@ -42,34 +44,64 @@ while (q) { // q points to an antenna of the same type as p unsigned qrow = TGrow(&tg, q), qcol = TGcol(&tg, q); int deltarow = (int)qrow - (int)prow; int deltacol = (int)qcol - (int)pcol; + int anticol = (int)pcol - deltacol; int antirow = (int)prow - deltarow; + unsigned *ff; if (TGvalid(&tg, (unsigned)anticol, (unsigned)antirow) && (*TGcharptr(&tg, (unsigned)anticol, (unsigned)antirow) != '\n')) { - unsigned *ff = antinode_find(anti, nanti, (unsigned)anticol, (unsigned)antirow); + ff = antinode_find(anti, nanti, (unsigned)anticol, (unsigned)antirow); if (!ff) { anti[nanti][0] = (unsigned)anticol; anti[nanti++][1] = (unsigned)antirow; } } + anticol = (int)qcol + deltacol; antirow = (int)qrow + deltarow; if (TGvalid(&tg, (unsigned)anticol, (unsigned)antirow) && (*TGcharptr(&tg, (unsigned)anticol, (unsigned)antirow) != '\n')) { - unsigned *ff = antinode_find(anti, nanti, (unsigned)anticol, (unsigned)antirow); + ff = antinode_find(anti, nanti, (unsigned)anticol, (unsigned)antirow); if (!ff) { anti[nanti][0] = (unsigned)anticol; anti[nanti++][1] = (unsigned)antirow; } } - q = strchr(q+1, *p); // next antenna of this type + + int k; + // q - delta is p; q -2delta is one afterwards, etc + // add q - (k)delta to anti2 while valid + k = 1; + while (TGvalid(&tg, (unsigned)((int)qcol-k*deltacol), (unsigned)((int)qrow-k*deltarow)) && (*TGcharptr(&tg, (unsigned)((int)qcol-k*deltacol), (unsigned)((int)qrow-k*deltarow)) != '\n')) { + ff = antinode_find(anti2, nanti2, (unsigned)((int)qcol-k*deltacol), (unsigned)((int)qrow-k*deltarow)); + if (!ff) { + anti2[nanti2][0] = (unsigned)((int)qcol-k*deltacol); + anti2[nanti2++][1] = (unsigned)((int)qrow-k*deltarow); + } + k++; + } + // p + delta is q; p +2delta is one afterwards, etc + // add p + (k)delta to anti2 while valid + k = 1; + while (TGvalid(&tg, (unsigned)((int)pcol+k*deltacol), (unsigned)((int)prow+k*deltarow)) && (*TGcharptr(&tg, (unsigned)((int)pcol+k*deltacol), (unsigned)((int)prow+k*deltarow)) != '\n')) { + ff = antinode_find(anti2, nanti2, (unsigned)((int)pcol+k*deltacol), (unsigned)((int)prow+k*deltarow)); + if (!ff) { + anti2[nanti2][0] = (unsigned)((int)pcol+k*deltacol); + anti2[nanti2++][1] = (unsigned)((int)prow+k*deltarow); + } + k++; + } + + // next antenna of this type + q = strchr(q+1, *p); } p += 1; } } printf("There are %u antinodes within the map.\n", nanti); + printf("There are %u resonant antinodes within the map.\n", nanti2); } /* === aoc202407 ======================================================= Part one looks easy Part two also easy