Advent of Code

Check-in [6c8d6b453f]
Login

Check-in [6c8d6b453f]

Overview
Comment:202408 1st star
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6c8d6b453fa1f933f6155a2e8c3a51c01fbe6140a860e052bf8dc69849037a50
User & Date: nnz on 2024-12-08 16:02:59
Other Links: manifest | tags
Context
2024-12-08
17:11
202408 2nd star check-in: 9395df6993 user: nnz tags: trunk
16:02
202408 1st star check-in: 6c8d6b453f user: nnz tags: trunk
15:25
202408 found the antinodes; counting to do check-in: ce30145a0d user: nnz tags: trunk
Changes

Modified Makefile from [5fd029b772] to [e5558dd887].

1
2
3
4
5
6
7
8
all: aoc aocdbg aocclang aoctcc

clean:
	rm aocdbg aoc aocclang aoctcc

aocdbg: *.c *.h
	gcc -std=c99 -pedantic -pedantic-errors \
	-Werror -Wall -Wextra \
|







1
2
3
4
5
6
7
8
all: aocdbg aoc aocclang aoctcc

clean:
	rm aocdbg aoc aocclang aoctcc

aocdbg: *.c *.h
	gcc -std=c99 -pedantic -pedantic-errors \
	-Werror -Wall -Wextra \

Modified aoc2024.c from [ffa5f1e4f3] to [98ee960fa7].

10
11
12
13
14
15
16








17
18
19
20
21
22
23
24
25
26
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
/* === aoc202408 =======================================================
   Oh! another square of text!
   Idea: for all points p with an antenna find all points q>p with
a corresponding frequency. For each such pair calculate the 2 antinodes
and add the resulting points (if not there already) to an array.
   The answer to Part One is the number of elements in the array
===================================================================== */









void aoc202408(char *data, size_t len) {
    (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'
    #if 0
    unsigned anti[256][2]; // locations of antinodes
    unsigned nanti = 0;
    #endif

    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;
                printf("p is at (C:%u, R:%u); q is at (C:%u, R:%u) ... delta: (C: %d, R: %d)\n",
                      pcol, prow, qcol, qrow, deltacol, deltarow);
                int col1 = (int)pcol - deltacol, row1 = (int)prow - deltarow;
                if (TGvalid(&tg, row1, col1)) {
                    printf("antinode at (C:%u, R:%u)\n", col1, row1);
                }
                int col2 = (int)qcol + deltacol, row2 = (int)qrow + deltarow;
                if (TGvalid(&tg, row2, col2)) {
                    printf("antinode at (C:%u, R:%u)\n", col2, row2);

                }
                q = strchr(q+1, *p); // next antenna of this type
            }
            p += 1;
        }
    }
    #if 0
    unsigned size = 1 + strchr(data, '\n') - data;
    for (unsigned row = 0; row < size-1; row++) {

        for (unsigned col = 0; col < size-1; col++) {
            char *p = data + linearize2d(size, row, col);
            if (*p != '.') {
                char *q = strchr(p + 1, *p);
                while (q) {
                    printf("found '%c'('%c')\n", *p, *q);
                    ptrdiff_t delta = q - p;
#if 0
                    int row_pos = (int)(linear_pos / size);
                    int col_pos = (int)(linear_pos % size);
#endif
                    if (p >= data + delta) {
                        // antinode
                        anti[nanti][0] = row(p - delta);
                        anti[nanti++][1] = col(p - delta);
                    }
                    if (q < "end of data" - delta) {
                        // antinode
                        anti[nanti][0] = row(q + delta);
                        anti[nanti++][1] = col(q + delta);
                    }
                    q = strchr(q + 1, *p);
                }

            }
        }
    }
    printf("There are %u antinodes within the map.\n", nanti);
    #endif
}

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








>
>
>
>
>
>
>
>







<
|

<













<
|
|
|
|
<
<
|
|
>
|
<
|
|
<
<
<
<
|
>
|
<
<
<
<
<
<
|
<
<
<
<
<
|
|

<
<
<
<
|
|
|
>
|
|
<

<







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
/* === aoc202408 =======================================================
   Oh! another square of text!
   Idea: for all points p with an antenna find all points q>p with
a corresponding frequency. For each such pair calculate the 2 antinodes
and add the resulting points (if not there already) to an array.
   The answer to Part One is the number of elements in the array
===================================================================== */

static unsigned *antinode_find(unsigned (*a)[2], size_t n,
                               unsigned col, unsigned row) {
    for (size_t k = 0; k < n; k++) {
        if ((a[k][0] == col) && (a[k][1] == row)) return a[k];
    }
    return NULL;
}

void aoc202408(char *data, size_t len) {
    (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
 ===================================================================== */

Modified aocutils.c from [462345b34b] to [e6035b6b40].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "aocutils.h"

bool TGvalid(struct TextGrid *tg, unsigned row, unsigned col) {
    if (row >= tg->rows) return false;
    if (col >= tg->cols) return false;
    return true;
}
char *TGcharptr(struct TextGrid *tg, unsigned row, unsigned col) {
    if (!TGvalid(tg, row, col)) return NULL;
    return tg->data + (row * tg->cols) + col;
}
unsigned TGcol(struct TextGrid *tg, char *p) {
    return (p - tg->data) % tg->cols;
}
unsigned TGrow(struct TextGrid *tg, char *p) {
    return (p - tg->data) / tg->cols;





|




|
|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "aocutils.h"

bool TGvalid(struct TextGrid *tg, unsigned col, unsigned row) {
    if (row >= tg->rows) return false;
    if (col >= tg->cols) return false;
    return true;
}
char *TGcharptr(struct TextGrid *tg, unsigned col, unsigned row) {
    if (!TGvalid(tg, col, row)) return NULL;
    return tg->data + (row * tg->cols) + col;
}
unsigned TGcol(struct TextGrid *tg, char *p) {
    return (p - tg->data) % tg->cols;
}
unsigned TGrow(struct TextGrid *tg, char *p) {
    return (p - tg->data) / tg->cols;

Modified aocutils.h from [8f6a78c0fe] to [11e9112422].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef AOCUTILS_H_INCLUDED
#define AOCUTILS_H_INCLUDED

#include <stdbool.h>

struct TextGrid {
    unsigned rows, cols;
    char *data; // may have '\n' at end of rows
};

bool TGvalid(struct TextGrid *tg, unsigned row, unsigned col);
char *TGcharptr(struct TextGrid *tg, unsigned row, unsigned col);
unsigned TGcol(struct TextGrid *tg, char *p);
unsigned TGrow(struct TextGrid *tg, char *p);

size_t linearize2d(unsigned width, unsigned row, unsigned col); // TODO
size_t text2array(unsigned **dst, const char *txt);
size_t slurp(char **dst, const char *filename);
unsigned distance(unsigned a, unsigned b);






|



|
|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef AOCUTILS_H_INCLUDED
#define AOCUTILS_H_INCLUDED

#include <stdbool.h>

struct TextGrid {
    unsigned cols, rows;
    char *data; // may have '\n' at end of rows
};

bool TGvalid(struct TextGrid *tg, unsigned col, unsigned row);
char *TGcharptr(struct TextGrid *tg, unsigned col, unsigned row);
unsigned TGcol(struct TextGrid *tg, char *p);
unsigned TGrow(struct TextGrid *tg, char *p);

size_t linearize2d(unsigned width, unsigned row, unsigned col); // TODO
size_t text2array(unsigned **dst, const char *txt);
size_t slurp(char **dst, const char *filename);
unsigned distance(unsigned a, unsigned b);