Advent of Code

Diff
Login

Diff

Differences From Artifact [f1c77ad8a8]:

To Artifact [355da142b3]:


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

static void reorder(unsigned *a, size_t na, unsigned (*r)[2], size_t nr) {




}

static const unsigned *vfind(unsigned v, const unsigned *a, size_t n) {
    for (size_t k = 0; k < n; k++) {
        if (a[k] == v) return a + k;
    }
    return NULL;
}

void aoc202405(char *data, size_t len) {
    (void)len; // unused argument
    unsigned pagepair[1200][2], npp = 0; // 1200 works for me

    char *line = strtok(data, "\n");
    while (strchr(line, '|')) {
        char *err;
        pagepair[npp][0] = strtoul(line, &err, 10);

        err += 1; // skip '|'
        pagepair[npp][1] = strtoul(err, &err, 10);

        npp++;
        line = strtok(NULL, "\n");
    }





    unsigned update[32], nupdates; // 32 works for me
    unsigned accumsum = 0;
    do {
        nupdates = 0;
        char *err = line;
        for (;;) {
            update[nupdates++] = strtoul(err, &err, 10);
            if (*err == ',') err++;
            else break;
        }
        if (nupdates % 2 == 0) printf("even number of updates found!\n");
        unsigned accum = update[nupdates / 2], accumsum2 = 0;
        for (size_t k = 0; k < npp; k++) {
            const unsigned *before = vfind(pagepair[k][0], update, nupdates);
            if (!before) continue;
            const unsigned *after = vfind(pagepair[k][1], update, nupdates);
            if (!after) continue;
            if (before > after) {
                accum = 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
55
56
57
58
59
60
61
62
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aocdailies.h"
#include "aocutils.h"

static void reorder(unsigned *a, size_t na, unsigned (*r)[2], size_t nr) {
    (void)a;
    (void)na;
    (void)r;
    (void)nr;
}

static const unsigned *vfind(unsigned v, const unsigned *a, size_t n) {
    for (size_t k = 0; k < n; k++) {
        if (a[k] == v) return a + k;
    }
    return NULL;
}

void aoc202405(char *data, size_t len) {
    (void)len; // unused argument
    unsigned pagepair[1200][2], npp = 0; // 1200 works for me
    unsigned ppcount[100][2] = {0}; // assume all page numbers < 100
    char *line = strtok(data, "\n");
    while (strchr(line, '|')) {
        char *err;
        pagepair[npp][0] = strtoul(line, &err, 10);
        ppcount[pagepair[npp][0]][0]++;
        err += 1; // skip '|'
        pagepair[npp][1] = strtoul(err, &err, 10);
        ppcount[pagepair[npp][1]][1]++;
        npp++;
        line = strtok(NULL, "\n");
    }
    for (unsigned k = 0; k < 100; k++) {
        if (ppcount[k][0] + ppcount[k][1])
              printf("%u: [%u, %u]  ", k, ppcount[k][0], ppcount[k][1]);
    }
    printf("\n");
    unsigned update[32], nupdates; // 32 works for me
    unsigned accumsum = 0, accumsum2 = 0;
    do {
        nupdates = 0;
        char *err = line;
        for (;;) {
            update[nupdates++] = strtoul(err, &err, 10);
            if (*err == ',') err++;
            else break;
        }
        if (nupdates % 2 == 0) printf("even number of updates found!\n");
        unsigned accum = update[nupdates / 2];
        for (size_t k = 0; k < npp; k++) {
            const unsigned *before = vfind(pagepair[k][0], update, nupdates);
            if (!before) continue;
            const unsigned *after = vfind(pagepair[k][1], update, nupdates);
            if (!after) continue;
            if (before > after) {
                accum = 0;