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
|
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;
}
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;
for (unsigned row = 0; row < size; row++) {
for (unsigned col = 0; col < size; col++) {
xmascount += xmasat(data, size, row, col);
}
}
printf("XMAS 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;
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
|
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
|
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)) {
Xmascount += Xmasat(data, size, row, col);
}
}
}
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;
|