Advent of Code

Check-in [5a3d05d86e]
Login

Check-in [5a3d05d86e]

Overview
Comment:202407 2nd star
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 5a3d05d86e2ebbf26cac5fe9035b82924a5a7889867a564e92ca6639256f7ea0
User & Date: nnz on 2024-12-07 10:39:03
Other Links: manifest | tags
Context
2024-12-07
13:50
reworked operators insertion logic check-in: 9237d89a9a user: nnz tags: trunk
10:39
202407 2nd star check-in: 5a3d05d86e user: nnz tags: trunk
09:40
202407 1st star check-in: 965ad94c16 user: nnz tags: trunk
Changes

Modified aoc2024.c from [e5aa25e91a] to [d3378fca96].

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

/* === aoc202407 =======================================================
   Part one looks easy
===================================================================== */










static bool canbetrue(unsigned long long v, unsigned long long *a, size_t n) {
    // for startes let's assume no overflowing
    if (n == 1) return (v == *a);
    unsigned tmp = a[1];
    a[1] = a[0] + tmp;
    if (canbetrue(v, a+1, n-1)) return true;
    a[1] = a[0] * tmp;
    if (canbetrue(v, a+1, n-1)) return true;














    a[1] = tmp;
    return false;
}

void aoc202407(char *data, size_t len) {
    (void)len; // unused argument
    unsigned long long calibrationtotal = 0;
    char *line = strtok(data, "\n");
    unsigned long long number[50], nnumber;
    while (line) {
        char *err;
        unsigned long long testvalue = strtoull(line, &err, 10);
        err += 1; // skip ':'
        nnumber = 0;
        while (*err == ' ') {
            number[nnumber++] = strtoul(err, &err, 10);
        }
        if (canbetrue(testvalue, number, nnumber)) calibrationtotal += testvalue;





        line = strtok(NULL, "\n");
    }
    printf("The calibration total is {%llu}.\n", calibrationtotal);

}

/* === 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`
Part Two: brute force? It likely is faster than coming up with an algo
   Let's go: save map, for every available position put in an obstacle,













>
>
>
>
>
>
>
>
>

<

|

|

|
>
>
>
>
>
>
>
>
>
>
>
>
>
>






|










|
>
>
>
>
>



>







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

/* === aoc202407 =======================================================
   Part one looks easy
===================================================================== */

static unsigned long long concat(unsigned long long a, unsigned long long b) {
    unsigned long long r = a, t = b;
    while (b) {
        r *= 10;
        b /= 10;
    }
    return r + t;
}

static bool canbetrue(unsigned long long v, unsigned long long *a, size_t n) {

    if (n == 1) return (v == *a);
    unsigned long long tmp = a[1];
    a[1] = a[0] + tmp;
    if (canbetrue(v, a+1, n-1)) { a[1] = tmp; return true; }
    a[1] = a[0] * tmp;
    if (canbetrue(v, a+1, n-1)) { a[1] = tmp; return true; }
    a[1] = tmp;
    return false;
}

static bool canbetrue3(unsigned long long v, unsigned long long *a, size_t n) {
    // still, let's assume no unsigned long long overflowing
    if (n == 1) return (v == *a);
    unsigned long long tmp = a[1];
    a[1] = a[0] + tmp;
    if (canbetrue3(v, a+1, n-1)) { a[1] = tmp; return true; }
    a[1] = a[0] * tmp;
    if (canbetrue3(v, a+1, n-1)) { a[1] = tmp; return true; }
    a[1] = concat(a[0], tmp);
    if (canbetrue3(v, a+1, n-1)) { a[1] = tmp; return true; }
    a[1] = tmp;
    return false;
}

void aoc202407(char *data, size_t len) {
    (void)len; // unused argument
    unsigned long long calibrationtotal = 0, calibration3total = 0;
    char *line = strtok(data, "\n");
    unsigned long long number[50], nnumber;
    while (line) {
        char *err;
        unsigned long long testvalue = strtoull(line, &err, 10);
        err += 1; // skip ':'
        nnumber = 0;
        while (*err == ' ') {
            number[nnumber++] = strtoul(err, &err, 10);
        }
        if (canbetrue(testvalue, number, nnumber)) {
            calibrationtotal += testvalue;
        }
        if (canbetrue3(testvalue, number, nnumber)) {
            calibration3total += testvalue;
        }
        line = strtok(NULL, "\n");
    }
    printf("The calibration total is {%llu}.\n", calibrationtotal);
    printf("The calibration total with three operators is {%llu}.\n", calibration3total);
}

/* === 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`
Part Two: brute force? It likely is faster than coming up with an algo
   Let's go: save map, for every available position put in an obstacle,