Advent of Code

Check-in [acf723019c]
Login

Check-in [acf723019c]

Overview
Comment:202417 1st star
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: acf723019ca83d3f5340fa841ea4815313a577f794c1f69c66f46720c01cb31e
User & Date: nnz on 2024-12-26 11:56:58
Other Links: manifest | tags
Context
2025-01-09
19:02
202417 2nd star check-in: 38c17bcc28 user: nnz tags: trunk
2024-12-26
11:56
202417 1st star check-in: acf723019c user: nnz tags: trunk
2024-12-25
15:31
202422 1st star check-in: 8769d8509e user: nnz tags: trunk
Changes

Modified Makefile from [e5558dd887] to [c9a2b8ef76].

1

2
3
4
5
6
7
8

1
2
3
4
5
6
7
8
-
+







all: aocdbg aoc aocclang aoctcc
all: aoc aocdbg aoc aocclang aoctcc

clean:
	rm aocdbg aoc aocclang aoctcc

aocdbg: *.c *.h
	gcc -std=c99 -pedantic -pedantic-errors \
	-Werror -Wall -Wextra \

Modified aoc.c from [0723e06f03] to [d6d5abcd14].




1
2
3
4
5
6
7
1
2
3
4
5
6
7
8
9
10
+
+
+







/* *********************************************************************
Repository: https://chiselapp.com/user/nnz/repository/AdventOfCode/index
********************************************************************* */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aocdailies.h" // prototypes for all aocYYYYdd functions!
#include "aocutils.h"

#define MAX_YEAR 2024

Modified aoc2024.c from [e64fb4bd5d] to [3996de5fa9].

31
32
33
34
35
36
37































































38
39
40
41
42
43
44
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







            secret = prune(mix(secret * 2048, secret));
        }
        sum2000 += secret;
        key = strtok(NULL, "\n");
    }
    printf("The sum of all 2000th secrets is {%llu}.\n", sum2000);
}

/* === aoc202417 =======================================================
===================================================================== */

static unsigned combo(unsigned operand, unsigned long long r[3]) {
    switch (operand) {
        default: fprintf(stderr, "Nope!\n"); exit(EXIT_FAILURE);
        case 0: return 0;
        case 1: return 1;
        case 2: return 2;
        case 3: return 3;
        case 4: return (unsigned)(r[0] & 0xFFFFFFFF);
        case 5: return (unsigned)(r[1] & 0xFFFFFFFF);
        case 6: return (unsigned)(r[2] & 0xFFFFFFFF);
    }
}

static unsigned instruction(unsigned ip, unsigned opcode, unsigned operand,
                            unsigned long long r[3],
                            char *out, size_t *outlen) {
    unsigned nextip = ip + 2;
    switch (opcode) {
        default: case 3: if (r[0]) { nextip = operand; } break;
        case 0: r[0] = r[0] / (1 << combo(operand, r)); break;
        case 1: r[1] = r[1] ^ operand; break;
        case 2: r[1] = combo(operand, r) % 8; break;
        case 4: r[1] = r[1] ^ r[2]; break;
        case 5: if (*outlen) { out[*outlen] = ','; *outlen += 1; }
                out[*outlen] = combo(operand, r) % 8 + '0';
                *outlen += 1;
                break;
        case 6: r[1] = r[0] / (1 << combo(operand, r)); break;
        case 7: r[2] = r[0] / (1 << combo(operand, r)); break;
    }
    return nextip;
}

void aoc202417(char *data, size_t len) {
    (void)len; //unused argument
    unsigned long long reg[4]; // A, B, and C ... and copy of A for Part Two
    char *dta = strstr(data, " A: "), *err;
    reg[0] = strtoull(dta + 4, &err, 10);
    dta = strstr(err, " B: ");
    reg[1] = strtoull(dta + 4, &err, 10);
    dta = strstr(err, " C: ");
    reg[2] = strtoull(dta + 4, &err, 10);
    dta = strstr(err, "Program: ") + 9;
    unsigned p[100] = {0}, np = 0;
    while (dta && *dta) {
        unsigned v = strtoul(dta, &err, 10);
        p[np++] = v;
        if (*err == 0) dta = NULL;
        else dta = err + 1; // skip comma
    }
    char output[1000];
    size_t outlen = 0;
    unsigned ip = 0;
    while (ip + 1 < np) {
        ip = instruction(ip, p[ip], p[ip+1], reg, output, &outlen);
    }
    output[outlen] = 0;
    printf("Program's output is {%s}\n", output);
}

/* === aoc202409 =======================================================
   WOW! input consists of a 20,000 long string of numbers!
   Make an array of blocks, each with either -1 or the fileid
   For Part Two: make an array of blocks with the encoded value of
(fileid * 10) + length, or, when empty, the negative length
===================================================================== */

Modified aocdailies.c from [569de5a694] to [3b7dbf3aef].

1
2
3
4
5
6
7
8
9
10

11
12
13
14
15
16
17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18










+







#include <stddef.h>
#include "aocdailies.h"

aocfunc *aocselect(unsigned y, unsigned d) {
    aocfunc *p;
    switch (y * 100 + d) {
        default: p = NULL; break;

        //   YYYYdd ==>  aocYYYYdd
        case 202422: p = aoc202422; break;
        case 202417: p = aoc202417; break;
        case 202409: p = aoc202409; break;
        case 202408: p = aoc202408; break;
        case 202407: p = aoc202407; break;
        case 202406: p = aoc202406; break;
        case 202405: p = aoc202405; break;
        case 202404: p = aoc202404; break;
        case 202403: p = aoc202403; break;

Modified aocdailies.h from [e496b7058b] to [c3a4c5f14f].

1
2
3
4
5
6
7
8
9

10
11
12
13
14
15
16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17









+







#ifndef AOCDAILIES_H_INCLUDED
#define AOCDAILIES_H_INCLUDED

#include <stddef.h>

typedef void aocfunc(char *, size_t);
aocfunc *aocselect(unsigned, unsigned);

aocfunc aoc202422;
aocfunc aoc202417;
aocfunc aoc202409;
aocfunc aoc202408;
aocfunc aoc202407;
aocfunc aoc202406;
aocfunc aoc202405;
aocfunc aoc202404;
aocfunc aoc202403;