Advent of Code

Diff
Login

Diff

Differences From Artifact [e64fb4bd5d]:

To Artifact [3996de5fa9]:


31
32
33
34
35
36
37































































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
































































/* === 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
===================================================================== */







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
===================================================================== */