Advent of Code

Diff
Login

Diff

Differences From Artifact [bb8df23888]:

To Artifact [9785441a22]:


1
2
3
4
5
6
7
8
9





10
11
12
13



14
15
16
17
18
19
20
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aocdailies.h"
#include "aocutils.h"






void aoc202406(char *data, size_t len) {
    (void)data;
    (void)len;
}




static int ppcmp(unsigned a, unsigned b, unsigned (*r)[2], size_t nr) {
    for (size_t k = 0; k < nr; k++) {
        if ((a == r[k][0]) && (b == r[k][1])) return -1;
        if ((a == r[k][1]) && (b == r[k][0])) return 1;
    }
    return 0;









>
>
>
>
>




>
>
>







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

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

void aoc202406(char *data, size_t len) {
    (void)data;
    (void)len;
}

/* === aoc202405 =======================================================
===================================================================== */

static int ppcmp(unsigned a, unsigned b, unsigned (*r)[2], size_t nr) {
    for (size_t k = 0; k < nr; k++) {
        if ((a == r[k][0]) && (b == r[k][1])) return -1;
        if ((a == r[k][1]) && (b == r[k][0])) return 1;
    }
    return 0;
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158



159
160
161
162
163
164
165
        }
        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);
}

static size_t rcmap(unsigned size, unsigned row, unsigned col) {
    return row * (size + 1) + col;
}

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[rcmap(size, (unsigned)((int)row + drow), (unsigned)((int)col + dcol))] != 'M') return false;
        if (data[rcmap(size, (unsigned)((int)row + 2*drow), (unsigned)((int)col + 2*dcol))] != 'A') return false;
        if (data[rcmap(size, (unsigned)((int)row + 3*drow), (unsigned)((int)col + 3*dcol))] != 'S') return false;
    } else {
        return false;
    }
    return true;
}

static unsigned xmasat(char *data, unsigned size, unsigned row, unsigned col) {
    unsigned finds = 0;
    if (data[rcmap(size, row, col)] != 'X') return 0;
    if (masat(data, size, row, col, -1, -1)) finds++;
    if (masat(data, size, row, col, -1,  0)) finds++;
    if (masat(data, size, row, col, -1, +1)) finds++;
    if (masat(data, size, row, col,  0, -1)) finds++;
    if (masat(data, size, row, col,  0, +1)) finds++;
    if (masat(data, size, row, col, +1, -1)) finds++;
    if (masat(data, size, row, col, +1,  0)) finds++;
    if (masat(data, size, row, col, +1, +1)) finds++;
    return finds;
}

static bool Xmasat(char *data, unsigned size, unsigned row, unsigned col) {
    unsigned tmp = 0;
    if (data[rcmap(size, row, col)] != 'A') return false;
    if (data[rcmap(size, row-1, col-1)] == 'M') tmp |= 1;
    if (data[rcmap(size, row-1, col-1)] == 'S') tmp |= 2;
    if (data[rcmap(size, row-1, col+1)] == 'M') tmp |= 4;
    if (data[rcmap(size, row-1, col+1)] == 'S') tmp |= 8;
    if (data[rcmap(size, row+1, col+1)] == 'M') tmp |= 1;
    if (data[rcmap(size, row+1, col+1)] == 'S') tmp |= 2;
    if (data[rcmap(size, row+1, col-1)] == 'M') tmp |= 4;
    if (data[rcmap(size, row+1, col-1)] == 'S') tmp |= 8;
    return (tmp == 15);
}

void aoc202404(char *data, size_t len) {
    (void)len; // unused argument
    // assume data is well-formatted and has the same number of rows and columns
    unsigned size = strchr(data, '\n') - data;
    #if 0 // check assumption
    printf("data has %u cols, first three rows start with %c%c, %c%c, and %c%c.\n",
          size, data[0], data[1],
          data[1*(size+1)], data[1*(size+1)+1],
          data[2*(size+1)], data[2*(size+1)+1]);
    printf("ends with %c%c.\n", data[(size-1)*(size+1) + size-2], data[(size-1)*(size+1)+size-1]);
    printf("data has %u cols, first three rows start with %c%c, %c%c, and %c%c.\n",
          size, data[rcmap(size, 0, 0)], data[rcmap(size, 0, 1)],
          data[rcmap(size, 1, 0)], data[rcmap(size, 1, 1)],
          data[rcmap(size, 2, 0)], data[rcmap(size, 2, 1)]);
    printf("ends with %c%c.\n", data[rcmap(size, size-1, size-2)], data[rcmap(size, size-1, size-1)]);
    #endif
    unsigned xmascount = 0, Xmascount = 0;
    for (unsigned row = 0; row < size; row++) {
        for (unsigned col = 0; col < size; col++) {
            xmascount += xmasat(data, size, row, col);
            if ((1 <= row) && (row < size - 1) && (1 <= col) && (col < size - 1)) {
                if (Xmasat(data, size, row, col)) Xmascount++;
            }
        }
    }
    printf("XMAS appears %u times in the little Elf's word search.\n", xmascount);
    printf("X-MAS appears %u times in the little Elf's word search.\n", Xmascount);
}




void aoc202403(char *data, size_t len) {
    (void)len; // unused argument
    unsigned sumproducts = 0, sumproducts2 = 0, term[2];
    char *rest = data;
    char *doleft = data;
    char *dorite = strstr(data + 1, "do()");







|
|
<





|
|
|








|













|
|
|
|
|
|
|
|
|














|
|
|
|













>
>
>







87
88
89
90
91
92
93
94
95

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
        }
        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;
        if (data[linearize2d(size+1, (unsigned)((int)row + 2*drow), (unsigned)((int)col + 2*dcol))] != 'A') return false;
        if (data[linearize2d(size+1, (unsigned)((int)row + 3*drow), (unsigned)((int)col + 3*dcol))] != 'S') return false;
    } else {
        return false;
    }
    return true;
}

static unsigned xmasat(char *data, unsigned size, unsigned row, unsigned col) {
    unsigned finds = 0;
    if (data[linearize2d(size+1, row, col)] != 'X') return 0;
    if (masat(data, size, row, col, -1, -1)) finds++;
    if (masat(data, size, row, col, -1,  0)) finds++;
    if (masat(data, size, row, col, -1, +1)) finds++;
    if (masat(data, size, row, col,  0, -1)) finds++;
    if (masat(data, size, row, col,  0, +1)) finds++;
    if (masat(data, size, row, col, +1, -1)) finds++;
    if (masat(data, size, row, col, +1,  0)) finds++;
    if (masat(data, size, row, col, +1, +1)) finds++;
    return finds;
}

static bool Xmasat(char *data, unsigned size, unsigned row, unsigned col) {
    unsigned tmp = 0;
    if (data[linearize2d(size+1, row, col)] != 'A') return false;
    if (data[linearize2d(size+1, row-1, col-1)] == 'M') tmp |= 1;
    if (data[linearize2d(size+1, row-1, col-1)] == 'S') tmp |= 2;
    if (data[linearize2d(size+1, row-1, col+1)] == 'M') tmp |= 4;
    if (data[linearize2d(size+1, row-1, col+1)] == 'S') tmp |= 8;
    if (data[linearize2d(size+1, row+1, col+1)] == 'M') tmp |= 1;
    if (data[linearize2d(size+1, row+1, col+1)] == 'S') tmp |= 2;
    if (data[linearize2d(size+1, row+1, col-1)] == 'M') tmp |= 4;
    if (data[linearize2d(size+1, row+1, col-1)] == 'S') tmp |= 8;
    return (tmp == 15);
}

void aoc202404(char *data, size_t len) {
    (void)len; // unused argument
    // assume data is well-formatted and has the same number of rows and columns
    unsigned size = strchr(data, '\n') - data;
    #if 0 // check assumption
    printf("data has %u cols, first three rows start with %c%c, %c%c, and %c%c.\n",
          size, data[0], data[1],
          data[1*(size+1)], data[1*(size+1)+1],
          data[2*(size+1)], data[2*(size+1)+1]);
    printf("ends with %c%c.\n", data[(size-1)*(size+1) + size-2], data[(size-1)*(size+1)+size-1]);
    printf("data has %u cols, first three rows start with %c%c, %c%c, and %c%c.\n",
          size, data[linearize2d(size+1, 0, 0)], data[linearize2d(size+1, 0, 1)],
          data[linearize2d(size+1, 1, 0)], data[linearize2d(size+1, 1, 1)],
          data[linearize2d(size+1, 2, 0)], data[linearize2d(size+1, 2, 1)]);
    printf("ends with %c%c.\n", data[linearize2d(size+1, size-1, size-2)], data[linearize2d(size+1, size-1, size-1)]);
    #endif
    unsigned xmascount = 0, Xmascount = 0;
    for (unsigned row = 0; row < size; row++) {
        for (unsigned col = 0; col < size; col++) {
            xmascount += xmasat(data, size, row, col);
            if ((1 <= row) && (row < size - 1) && (1 <= col) && (col < size - 1)) {
                if (Xmasat(data, size, row, col)) Xmascount++;
            }
        }
    }
    printf("XMAS appears %u times in the little Elf's word search.\n", xmascount);
    printf("X-MAS appears %u times in the little Elf's word search.\n", Xmascount);
}

/* === aoc202403 =======================================================
===================================================================== */

void aoc202403(char *data, size_t len) {
    (void)len; // unused argument
    unsigned sumproducts = 0, sumproducts2 = 0, term[2];
    char *rest = data;
    char *doleft = data;
    char *dorite = strstr(data + 1, "do()");
199
200
201
202
203
204
205



206
207
208
209
210
211
212
            break;
        }
    }
    printf("The sum of the products is {%u}.\n", sumproducts);
    printf("The sum of the products with conditionals is {%u}.\n", sumproducts2);
}




static bool safereport(unsigned *v, size_t nv) {
    int dir = 1;               // ascending
    if (v[0] > v[1]) dir = -1; // descending
    for (size_t k = 1; k < nv; k++) {
        if (v[k-1] == v[k]) return false;
        if (distance(v[k-1], v[k]) > 3) return false;
        if (dir == -1) {







>
>
>







209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
            break;
        }
    }
    printf("The sum of the products is {%u}.\n", sumproducts);
    printf("The sum of the products with conditionals is {%u}.\n", sumproducts2);
}

/* === aoc202402 =======================================================
===================================================================== */

static bool safereport(unsigned *v, size_t nv) {
    int dir = 1;               // ascending
    if (v[0] > v[1]) dir = -1; // descending
    for (size_t k = 1; k < nv; k++) {
        if (v[k-1] == v[k]) return false;
        if (distance(v[k-1], v[k]) > 3) return false;
        if (dir == -1) {
243
244
245
246
247
248
249



250
251
252
253
254
255
256
        }
        free(arr);
        line = strtok(NULL, "\n");
    }
    printf("There are %u safe reports.\n", safe);
    printf("There are %u safe reports with the Problem Dampener.\n", safe2);
}




static int delta(const void *a, const void *b) {
    const unsigned *aa = a;
    const unsigned *bb = b;
    if (*aa > *bb) return 1;
    if (*aa < *bb) return -1;
    return 0;







>
>
>







256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
        }
        free(arr);
        line = strtok(NULL, "\n");
    }
    printf("There are %u safe reports.\n", safe);
    printf("There are %u safe reports with the Problem Dampener.\n", safe2);
}

/* === aoc202401 =======================================================
===================================================================== */

static int delta(const void *a, const void *b) {
    const unsigned *aa = a;
    const unsigned *bb = b;
    if (*aa > *bb) return 1;
    if (*aa < *bb) return -1;
    return 0;