Advent of Code

Diff
Login

Diff

Differences From Artifact [98ee960fa7]:

To Artifact [05505307b8]:


27
28
29
30
31
32
33


34
35
36
37
38
39
40
41
42
43
44
45
46

47
48

49
50
51
52
53
54
55

56
57
58
59
60
61
62
63
64


























65
66
67
68
69
70

71
72
73
74
75
76
77
    (void)len; // unused argument
    struct TextGrid tg;
    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;



    char *p = data;
    while (*p) {
        while ((*p == '.') || (*p == '\n')) p++;
        if (*p) {
            // p points to an antenna
            unsigned prow = TGrow(&tg, p), pcol = TGcol(&tg, p);
            char *q = strchr(p+1, *p);
            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;

                if (TGvalid(&tg, (unsigned)anticol, (unsigned)antirow) && (*TGcharptr(&tg, (unsigned)anticol, (unsigned)antirow) != '\n')) {
                    unsigned *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);
                    if (!ff) {
                        anti[nanti][0] = (unsigned)anticol;
                        anti[nanti++][1] = (unsigned)antirow;
                    }
                }


























                q = strchr(q+1, *p); // next antenna of this type
            }
            p += 1;
        }
    }
    printf("There are %u antinodes within the map.\n", nanti);

}

/* === aoc202407 =======================================================
   Part one looks easy
   Part two also easy
 ===================================================================== */








>
>













>


>

|





>



|





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|





>







27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    (void)len; // unused argument
    struct TextGrid tg;
    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) {
            // p points to an antenna
            unsigned prow = TGrow(&tg, p), pcol = TGcol(&tg, p);
            char *q = strchr(p+1, *p);
            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')) {
                    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')) {
                    ff = antinode_find(anti, nanti, (unsigned)anticol, (unsigned)antirow);
                    if (!ff) {
                        anti[nanti][0] = (unsigned)anticol;
                        anti[nanti++][1] = (unsigned)antirow;
                    }
                }

                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
 ===================================================================== */