1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aocdailies.h"
#include "aocutils.h"
/* === aoc202503 =======================================================
===================================================================== */
static long long unsigned maxj(const unsigned char *b, unsigned blen, unsigned nlen) {
if (nlen == 0) return 0;
const unsigned char *newb = b + 1;
unsigned newlen = blen - 1;
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
80
81
|
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aocdailies.h"
#include "aocutils.h"
#if 0
/* === aocYYYYDD =======================================================
===================================================================== */
void aocYYYYDD(char *data, size_t len) {
(void)len; // unused argument
}
#endif
/* === aoc202504 =======================================================
===================================================================== */
static unsigned evolve(struct RectangularMap *rm, unsigned src) {
unsigned changes = 0;
unsigned dst = 1 - src;
for (int row = 0; row < rm->rows; row++) {
for (int col = 0; col < rm->cols; col++) {
char cc = RMcharat(rm + src, col, row);
char *destin = RMcharptr(rm + dst, col, row);
if (cc == '@') {
unsigned nearby = 0;
for (int deltarow = -1; deltarow <= 1; deltarow++) {
for (int deltacol = -1; deltacol <= 1; deltacol++) {
nearby += (RMcharat(rm + src, col+deltacol,
row+deltarow) == '@');
}
}
nearby -= 1; // remove self
if (nearby < 4) {
*destin = '.';
changes++;
} else {
*destin = '@';
}
} else if (cc != 0) {
*destin = '.';
} else {
*destin = 0; // shouldn't happen
}
}
}
return changes;
}
void aoc202504(char *data, size_t len) {
(void)len; // unused argument
struct RectangularMap rm[2] = {0};
while (*data) {
char *data2 = data;
while (*data != '\n') data++;
*data = 0; // erase newline
RMaddline(rm + 0, data2);
RMaddline(rm + 1, data2); // set both rm's to the same thing
*data++ = '\n'; // unerase newline and skip it
}
unsigned part1 = evolve(rm, 0); // evolve from rm[0] to rm[1]
unsigned part2 = part1, src = 1;
for (;;) {
unsigned tmp = evolve(rm, src); // keep evolving
if (tmp == 0) break; // STOP
src = 1 - src; // back and forth
part2 += tmp; // add changes in this round
}
printf("P1: %u; P2: %u\n", part1, part2);
RMfree(rm);
RMfree(rm + 1);
}
/* === aoc202503 =======================================================
===================================================================== */
static long long unsigned maxj(const unsigned char *b, unsigned blen, unsigned nlen) {
if (nlen == 0) return 0;
const unsigned char *newb = b + 1;
unsigned newlen = blen - 1;
|