Advent of Code

Check-in [e8b841e9cf]
Login

Check-in [e8b841e9cf]

Overview
Comment:202403 2nd star
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: e8b841e9cfe6d5f7a81dbd5c8573e865b0c270dbbae46ca4e5b91301f7e07679
User & Date: nnz on 2024-12-03 10:36:29
Original Comment: day 3, part 2 solved
Other Links: manifest | tags
Context
2024-12-03
13:38
changed order within dailies check-in: 3bac9b02b1 user: nnz tags: trunk
10:36
202403 2nd star check-in: e8b841e9cf user: nnz tags: trunk
10:00
202403 1st star check-in: 90b164fce2 user: nnz tags: trunk
Changes

Modified aoc2024.c from [89e5ae23d6] to [1fed90368b].

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
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, term[2];
    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;