Advent of Code

Check-in [149ea78401]
Login

Check-in [149ea78401]

Overview
Comment:202405 2nd star
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 149ea784019c8dd3092e699a93145b56d27cc7878624448edfee4299c50015fa
User & Date: nnz on 2024-12-05 16:55:13
Original Comment: 202405 -- 2 stars
Other Links: manifest | tags
Context
2024-12-05
20:38
prepare for tomorrow, start work on 201504 check-in: d415c28af8 user: nnz tags: trunk
16:55
202405 2nd star check-in: 149ea78401 user: nnz tags: trunk
14:39
202405 -- new idea: count pages positions check-in: fccbd87615 user: nnz tags: trunk
Changes

Modified aoc2024.c from [355da142b3] to [72b0bedd28].

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
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









+
+
+
+
+
+
+
+
+

-
+
-
-
-
+
+
+
+
+
+
+
+












-




-


-



-
-
-
-
-







#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 int ppcmp(unsigned a, unsigned b, unsigned (*r)[2], size_t nr) {
    for (size_t k = 0; k < nr; k++) {
        if ((a == r[k][0]) && (b == r[k][1])) return -1;
        if ((a == r[k][1]) && (b == r[k][0])) return 1;
    }
    return 0;
}

// can't be bothered to do anything other than bubble sort
static void reorder(unsigned *a, size_t na, unsigned (*r)[2], size_t nr) {
    (void)a;
    for (size_t range = na; --range > 0; /*void*/) {
    (void)na;
    (void)r;
    (void)nr;
        for (size_t outer = 0; outer < range; outer++) {
            if (ppcmp(a[outer], a[outer+1], r, nr) > 0) {
                unsigned tmp = a[outer];
                a[outer] = a[outer+1];
                a[outer+1] = tmp;
            }
        }
    }
}

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);
64
65
66
67
68
69
70
71
72


73
74
75
76
77
78
79
70
71
72
73
74
75
76


77
78
79
80
81
82
83
84
85







-
-
+
+







                reorder(update, nupdates, pagepair, npp);
                accumsum2 += update[nupdates / 2];
                break;
            }
        }
        accumsum += accum;
    } while ((line = strtok(NULL, "\n")) != NULL);
    printf("The sum of middle update numbers is {%u}\n", accumsum);
    printf("The sum of middle update numbers for newly ordered updates is {%u}\n", accumsum2);
    printf("The sum of middle update numbers is {%u}.\n", accumsum);
    printf("The sum of middle update numbers for newly ordered updates is {%u}.\n", accumsum2);
}

static size_t rcmap(unsigned size, unsigned row, unsigned col) {
    return row * (size + 1) + col;
}

static bool masat(char *data, unsigned size, unsigned row, unsigned col, int drow, int dcol) {