Index: Makefile ================================================================== --- Makefile +++ Makefile @@ -1,6 +1,6 @@ -all: aocdbg aoc aocclang aoctcc +all: aoc aocdbg aoc aocclang aoctcc clean: rm aocdbg aoc aocclang aoctcc aocdbg: *.c *.h Index: aoc.c ================================================================== --- aoc.c +++ aoc.c @@ -1,5 +1,8 @@ +/* ********************************************************************* +Repository: https://chiselapp.com/user/nnz/repository/AdventOfCode/index +********************************************************************* */ #include #include #include #include "aocdailies.h" // prototypes for all aocYYYYdd functions! #include "aocutils.h" Index: aoc2024.c ================================================================== --- aoc2024.c +++ aoc2024.c @@ -33,10 +33,73 @@ 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 Index: aocdailies.c ================================================================== --- aocdailies.c +++ aocdailies.c @@ -6,10 +6,11 @@ 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; Index: aocdailies.h ================================================================== --- aocdailies.h +++ aocdailies.h @@ -5,10 +5,11 @@ typedef void aocfunc(char *, size_t); aocfunc *aocselect(unsigned, unsigned); aocfunc aoc202422; +aocfunc aoc202417; aocfunc aoc202409; aocfunc aoc202408; aocfunc aoc202407; aocfunc aoc202406; aocfunc aoc202405;