Advent of Code

Check-in [a382d65717]
Login

Check-in [a382d65717]

Overview
Comment:202408 wip
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: a382d65717d2acf95ca13dae7bda79873e55028bd071931e22c484b16f37b148
User & Date: nnz on 2024-12-08 13:22:07
Other Links: manifest | tags
Context
2024-12-08
13:23
redo some formatting check-in: 6d26cab7c2 user: nnz tags: trunk
13:22
202408 wip check-in: a382d65717 user: nnz tags: trunk
2024-12-07
14:41
fixed possible bug (3) check-in: 4ad4c64403 user: nnz tags: trunk
Changes

Modified aoc2024.c from [9685519f7e] to [8deec3b966].

1
2
3
4
5
6
7
8
9












































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
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aocdailies.h"
#include "aocutils.h"













































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

static unsigned long long concat(unsigned long long a, unsigned long long b) {
    unsigned long long r = a, t = b;
    while (b) {
        r *= 10;
        b /= 10;
    }
    return r + t;
}

static int operatorsrequired(unsigned long long v, unsigned long long *a, size_t n, int minop) {
    // assumes no unsigned long long overflowing
    if (n == 1) {
        if (v == *a) return minop;
        return 0;
    }
    unsigned long long tmp = a[1];
    int pplus, pmult, pconcat = 0;
    a[1] = a[0] + tmp;
    pplus = operatorsrequired(v, a+1, n-1, 2);
    a[1] = tmp;
    a[1] = a[0] * tmp;









>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



|


|
|
|
|
|
|



|
|
|
|







1
2
3
4
5
6
7
8
9
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
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aocdailies.h"
#include "aocutils.h"

/* === 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
TODO: I don't like my `linearize2d` thing! Thinking of a replacement
===================================================================== */

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

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

static unsigned long long concat(unsigned long long a, unsigned long long b) {
        unsigned long long r = a, t = b;
        while (b) {
                r *= 10;
                b /= 10;
        }
        return r + t;
}

static int operatorsrequired(unsigned long long v, unsigned long long *a, size_t n, int minop) {
        // assumes no unsigned long long overflowing
        if (n == 1) {
                if (v == *a) return minop;
                return 0;
    }
    unsigned long long tmp = a[1];
    int pplus, pmult, pconcat = 0;
    a[1] = a[0] + tmp;
    pplus = operatorsrequired(v, a+1, n-1, 2);
    a[1] = tmp;
    a[1] = a[0] * tmp;
73
74
75
76
77
78
79

80
81
82
83
84
85
86

/* === aoc202406 =======================================================
   Hmmm, I've done something like this already this year... on day 4
   I need rcmap() moved to aocutils ... done; now it's called `linearize2d`
Part Two: brute force? It likely is faster than coming up with an algo
   Let's go: save map, for every available position put in an obstacle,
   loop around until some place is visited 4? times or area is exited

===================================================================== */

static bool validpos(unsigned s, int r, int c) {
    if (r < 0) return false;
    if (c < 0) return false;
    if ((unsigned)r >= s - 1) return false;
    if ((unsigned)c >= s - 1) return false;







>







117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

/* === aoc202406 =======================================================
   Hmmm, I've done something like this already this year... on day 4
   I need rcmap() moved to aocutils ... done; now it's called `linearize2d`
Part Two: brute force? It likely is faster than coming up with an algo
   Let's go: save map, for every available position put in an obstacle,
   loop around until some place is visited 4? times or area is exited
TODO remove linearize2d
===================================================================== */

static bool validpos(unsigned s, int r, int c) {
    if (r < 0) return false;
    if (c < 0) return false;
    if ((unsigned)r >= s - 1) return false;
    if ((unsigned)c >= s - 1) return false;
230
231
232
233
234
235
236

237
238
239
240
241
242
243
        accumsum += accum;
    } while ((line = strtok(NULL, "\n")) != NULL);
    printf("The sum of middle update numbers is {%u}.\n", accumsum);
    printf("The sum of middle update numbers for newly ordered updates is {%u}.\n", accumsum2);
}

/* === aoc202404 =======================================================

===================================================================== */

static bool masat(char *data, unsigned size, unsigned row, unsigned col, int drow, int dcol) {
    int maxrow = (int)row + 3*drow;
    int maxcol = (int)col + 3*dcol;
    if ((0 <= maxrow) && (maxrow < (int)size) && (0 <= maxcol) && (maxcol < (int)size)) {
        if (data[linearize2d(size+1, (unsigned)((int)row + drow), (unsigned)((int)col + dcol))] != 'M') return false;







>







275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
        accumsum += accum;
    } while ((line = strtok(NULL, "\n")) != NULL);
    printf("The sum of middle update numbers is {%u}.\n", accumsum);
    printf("The sum of middle update numbers for newly ordered updates is {%u}.\n", accumsum2);
}

/* === aoc202404 =======================================================
TODO remove linearize2d
===================================================================== */

static bool masat(char *data, unsigned size, unsigned row, unsigned col, int drow, int dcol) {
    int maxrow = (int)row + 3*drow;
    int maxcol = (int)col + 3*dcol;
    if ((0 <= maxrow) && (maxrow < (int)size) && (0 <= maxcol) && (maxcol < (int)size)) {
        if (data[linearize2d(size+1, (unsigned)((int)row + drow), (unsigned)((int)col + dcol))] != 'M') return false;

Modified aocdailies.c from [e5e3eb3466] to [fb8ca294f1].

1
2
3
4
5
6
7
8
9

10
11
12
13
14
15
16
#include <stddef.h>
#include "aocdailies.h"

aocfunc *aocselect(unsigned y, unsigned d) {
    aocfunc *p;
    switch (y * 100 + d) {
        default: p = NULL; break;

        //   YYYYdd ==>  aocYYYYdd

        case 202407: p = aoc202407; break;
        case 202406: p = aoc202406; break;
        case 202405: p = aoc202405; break;
        case 202404: p = aoc202404; break;
        case 202403: p = aoc202403; break;
        case 202402: p = aoc202402; break;
        case 202401: p = aoc202401; break;









>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stddef.h>
#include "aocdailies.h"

aocfunc *aocselect(unsigned y, unsigned d) {
    aocfunc *p;
    switch (y * 100 + d) {
        default: p = NULL; break;

        //   YYYYdd ==>  aocYYYYdd
        case 202408: p = aoc202408; break;
        case 202407: p = aoc202407; break;
        case 202406: p = aoc202406; break;
        case 202405: p = aoc202405; break;
        case 202404: p = aoc202404; break;
        case 202403: p = aoc202403; break;
        case 202402: p = aoc202402; break;
        case 202401: p = aoc202401; break;

Modified aocdailies.h from [e05094a8ab] to [114faad3c2].

1
2
3
4
5
6
7
8

9
10
11
12
13
14
15
#ifndef AOCDAILIES_H_INCLUDED
#define AOCDAILIES_H_INCLUDED

#include <stddef.h>

typedef void aocfunc(char *, size_t);
aocfunc *aocselect(unsigned, unsigned);


aocfunc aoc202407;
aocfunc aoc202406;
aocfunc aoc202405;
aocfunc aoc202404;
aocfunc aoc202403;
aocfunc aoc202402;
aocfunc aoc202401;








>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef AOCDAILIES_H_INCLUDED
#define AOCDAILIES_H_INCLUDED

#include <stddef.h>

typedef void aocfunc(char *, size_t);
aocfunc *aocselect(unsigned, unsigned);

aocfunc aoc202408;
aocfunc aoc202407;
aocfunc aoc202406;
aocfunc aoc202405;
aocfunc aoc202404;
aocfunc aoc202403;
aocfunc aoc202402;
aocfunc aoc202401;