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
|
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aocutils.h"
void aoc202403(char *data, [[maybe_unused]] size_t len) {
int sumproducts = 0, term[2];
char *rest = data;
for (;;) {
char *mul = strstr(rest, "mul(");
if (mul) {
rest = mul + 4;
if (isdigit((unsigned char)rest[0])) {
char *err;
term[0] = (int)strtol(rest, &err, 10);
if (*err == ',') {
if (isdigit((unsigned char)err[1])) {
rest = err + 1;
term[1] = (int)strtol(rest, &err, 10);
if (*err == ')') {
sumproducts += term[0] * term[1];
rest = err + 1;
}
}
}
}
} else {
break;
}
}
printf("The sum of the products is {%d}.\n", sumproducts);
}
static int safereport(int *v, int nv) {
int dir = 1; // ascending
if (v[0] > v[1]) dir = -1; // descending
for (int k = 1; k < nv; k++) {
if (v[k-1] == v[k]) 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
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
|
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aocutils.h"
void aoc202403(char *data, [[maybe_unused]] size_t len) {
int sumproducts = 0, sumproducts2 = 0, term[2];
char *rest = data;
char *doleft = data;
char *dorite = strstr(data + 1, "do()");
char *dontleft = data;
char *dontrite = strstr(dontleft + 1, "don't()");
for (;;) {
char *mul = strstr(rest, "mul(");
if (mul) {
while (dorite && (mul > dorite)) {
doleft = dorite;
dorite = strstr(dorite + 1, "do()");
}
while (dontrite && (mul > dontrite)) {
dontleft = dontrite;
dontrite = strstr(dontrite + 1, "don't()");
}
rest = mul + 4;
if (isdigit((unsigned char)rest[0])) {
char *err;
term[0] = (int)strtol(rest, &err, 10);
if (*err == ',') {
if (isdigit((unsigned char)err[1])) {
rest = err + 1;
term[1] = (int)strtol(rest, &err, 10);
if (*err == ')') {
sumproducts += term[0] * term[1];
sumproducts2 += (doleft >= dontleft) * (term[0] * term[1]);
rest = err + 1;
}
}
}
}
} else {
break;
}
}
printf("The sum of the products is {%d}.\n", sumproducts);
printf("The sum of the products with conditionals is {%d}.\n", sumproducts2);
}
static int safereport(int *v, int nv) {
int dir = 1; // ascending
if (v[0] > v[1]) dir = -1; // descending
for (int k = 1; k < nv; k++) {
if (v[k-1] == v[k]) return 0;
|