The true spirit of delight, the exaltation, the sense of being more than Man, which is the touchstone of the highest excellence, is to be found in mathematics as surely as in poetry.
!inf:texttype:default
Previously referred to as ‘cnilespammer’, it turns out the code author wasn’t the spammer at all! To stop him from suing me for slander, I’ve dedicated a page to his venerable, malloc-addled brain.
unknown-cnile/ucnile-day01.cunknown-cnile/ucnile-day01.c
\#include <stdlib.h> \#include <stdio.h> typedef \_\_int128 num\_t; void die(char \*str) { fprintf(stderr, "[ERROR] %s\n", str); exit(-1); } void swap(num\_t \*a, num\_t \*b) { num\_t tmp = \*a; \*a = \*b; \*b = tmp; } // accending order void my\_qsort(num\_t \*a, size\_t len) { switch (len) { case 2: if (a[0] > a[1]) swap(a, a+1); case 0: case 1: return; default:; num\_t mid = a[0]; size\_t nxt = 1; for (size\_t i = 1; i < len; i++) { if (a[i] <= mid) { swap(a+nxt, a+i); nxt++; } } swap(a, a+nxt-1); my\_qsort(a, nxt-1); my\_qsort(a+nxt, len-nxt); } } int check\_for(num\_t \*a, size\_t len, num\_t val) { if (len == 0) return 0; else if (a[len/2] == val) return 1; else if (a[len/2] < val) return check\_for(a + len/2 + 1, (len-1)/2, val); else return check\_for(a, len/2, val); } \#define TARGET 99920044L \#define MAX\_DATA 1024L \* 1024L num\_t data\_store[MAX\_DATA]; size\_t data\_pos = 0; void part\_one() { my\_qsort(data\_store, data\_pos); for (size\_t i = 0; i < data\_pos; i++) { num\_t r = TARGET - data\_store[i]; if (check\_for(data\_store+i, data\_pos-i, r)) { printf("P1: %lld\n", (long long) (data\_store[i] \* r)); } } } void print\_num(num\_t n) { if (n == 0) { putchar(&\#39;0&\#39;); return; } char bank[512]; bank[511] = &\#39;\0&\#39;; char \*ptr = bank + 511; while (n != 0) { \*(--ptr) = (n % 10) + &\#39;0&\#39;; n /= 10; } printf("%s", ptr); } void part\_two() { my\_qsort(data\_store, data\_pos); for (size\_t i = 0; (i < data\_pos) && (data\_store[i] <= (TARGET/3)); i++) { num\_t r = TARGET - data\_store[i]; for (size\_t j = i + 1; (j < data\_pos) && (data\_store[j] <= (r/2)); j++) { num\_t rr = r - data\_store[j]; if (check\_for(data\_store+j+1, data\_pos-j-1, rr)) { printf("P2: "); print\_num(data\_store[i] \* data\_store[j] \* rr); printf("\n"); } } } } int main(int argc, char \*\*argv) { // read input FILE \*fd = fopen("bb.txt", "r"); if (fd == NULL) die("failed to read"); while (1) { if (data\_pos == MAX\_DATA) die("too many numbers"); long long n; switch (fscanf(fd, "%lld", &n)) { case EOF: if (ferror(fd)) die("failed to read"); goto done; case 0: die("failed to match"); } data\_store[data\_pos] = (num\_t) n; data\_pos++; } done: // part 2 part\_two(); return 0; }
unknown-cnile/ucnile-day05.cunknown-cnile/ucnile-day05.c
\#include <stdlib.h> \#include <stdio.h> \#include <string.h> \#include <sys/types.h> \#include <sys/stat.h> \#include <fcntl.h> \#include <sys/mman.h> \#include <unistd.h> \#include <limits.h> void die(char \*str) { fprintf(stderr, "[ERROR] %s\n", str); exit(-1); } FILE \*map\_file(const char \*filename) { int fd; if ((fd = open(filename, O\_RDONLY)) == -1) die("failed to open file"); struct stat fd\_stat; if (fstat(fd, &fd\_stat) == -1) die("failed to stat file"); void \*data; // never unmapped, probably fine if ((data = mmap(NULL, fd\_stat.st\_size, PROT\_READ, MAP\_PRIVATE, fd, 0)) == MAP\_FAILED) die("failed to map file"); close(fd); return fmemopen(data, fd\_stat.st\_size, "r"); } unsigned int xor\_z(unsigned int x) { switch (x % 4) { case 0: return x; case 1: return 1; case 2: return x + 1; case 3: return 0; } } unsigned int xor\_range(unsigned int min, unsigned int max) { // should underflow on min == 0, resulting in xor\_z(min - 1) == xor\_z(3) == 0 return xor\_z(min - 1) ^ xor\_z(max); } \#define SEAT\_CNT 1024 int main(int argc, char \*\*argv) { // read input FILE \*fd = map\_file("test.txt"); char data[16]; unsigned int min = UINT\_MAX; unsigned int max = 0; unsigned int acc = 0; while (fgets(data, 16, fd) != NULL) { unsigned int sid = 0; for (int i = 0; i < 10; i++) { sid <<= 1; sid |= ((data[i] >> 2) & 1); } sid ^= SEAT\_CNT - 1; if (sid > max) max = sid; if (sid < min) min = sid; acc ^= sid; } printf("P1: %u\n", max); printf("P2: %u\n", acc ^ xor\_range(min, max)); return 0; }
unknown-cnile/ucnile-day06.cunknown-cnile/ucnile-day06.c
\#include <stdlib.h> \#include <stdio.h> \#include <string.h> \#include <sys/types.h> \#include <sys/stat.h> \#include <fcntl.h> \#include <sys/mman.h> \#include <unistd.h> void die(char \*str) { fprintf(stderr, "[ERROR] %s\n", str); exit(-1); } FILE \*map\_file(const char \*filename) { int fd; if ((fd = open(filename, O\_RDONLY)) == -1) die("failed to open file"); struct stat fd\_stat; if (fstat(fd, &fd\_stat) == -1) die("failed to stat file"); void \*data; // never unmapped, probably fine if ((data = mmap(NULL, fd\_stat.st\_size, PROT\_READ, MAP\_PRIVATE, fd, 0)) == MAP\_FAILED) die("failed to map file"); close(fd); return fmemopen(data, fd\_stat.st\_size, "r"); } int main(int argc, char \*\*argv) { // read input FILE \*fd = map\_file("test.txt"); char data[64]; int any\_acc = 0; int all\_acc = 0; unsigned int cur\_any = 0; unsigned int cur\_all = ~0; while (fgets(data, 64, fd) != NULL) { if (data[0] < &\#39;a&\#39;) { any\_acc += \_\_builtin\_popcount(cur\_any); all\_acc += \_\_builtin\_popcount(cur\_all); cur\_any = 0; cur\_all = ~0; } else { unsigned int row = 0; for (int i = 0; data[i] >= &\#39;a&\#39;; i++) { row |= 1 << (data[i] - &\#39;a&\#39;); } cur\_any |= row; cur\_all &= row; } } any\_acc += \_\_builtin\_popcount(cur\_any); all\_acc += \_\_builtin\_popcount(cur\_all); printf("P1: %d\n", any\_acc); printf("P2: %d\n", all\_acc); return 0; }
unknown-cnile/ucnile-day07.cunknown-cnile/ucnile-day07.c
\#include <stdlib.h> \#include <stdio.h> \#include <string.h> \#include <sys/types.h> \#include <sys/stat.h> \#include <fcntl.h> \#include <sys/mman.h> \#include <unistd.h> \#include <stdint.h> void die(char \*str) { fprintf(stderr, "[ERROR] %s\n", str); exit(-1); } FILE \*map\_file(const char \*filename) { int fd; if ((fd = open(filename, O\_RDONLY)) == -1) die("failed to open file"); struct stat fd\_stat; if (fstat(fd, &fd\_stat) == -1) die("failed to stat file"); void \*data; // never unmapped, probably fine if ((data = mmap(NULL, fd\_stat.st\_size, PROT\_READ, MAP\_PRIVATE, fd, 0)) == MAP\_FAILED) die("failed to map file"); close(fd); return fmemopen(data, fd\_stat.st\_size, "r"); } \#define MAX\_DATA 4096 struct item\_entry { uint32\_t bag\_id; uint32\_t bag\_cnt; }; struct bag { uint32\_t bag\_id; int32\_t p1\_state; uint32\_t contain\_cnt; struct item\_entry contain[16]; }; struct bag data[MAX\_DATA]; int data\_len = 0; \#define MAX\_LINE 256 \#define MOD\_HASH(acc, c) acc = (((acc) << 5) - (acc)) ^ (c) int compare\_bag(const void \*a, const void \*b) { uint32\_t an = ((struct bag \*) a)->bag\_id; uint32\_t bn = ((struct bag \*) b)->bag\_id; // assume no collisions if (an < bn) return -1; if (an > bn) return 1; \_\_builtin\_trap(); } uint32\_t scan\_color(char \*\*s) { uint32\_t acc = 0; while (\*\*s != &\#39; &\#39;) { acc = ((acc << 5) - acc) ^ \*((\*s)++); } acc = ((acc << 5) - acc) ^ &\#39; &\#39;; (\*s)++; while (\*\*s > &\#39; &\#39;) { acc = ((acc << 5) - acc) ^ \*((\*s)++); } return acc; } int find\_bag\_idx(uint32\_t bag\_id) { int min = 0; int max = data\_len - 1; while (1) { int pick = (min + max) / 2; if (data[pick].bag\_id == bag\_id) return pick; else if (min == max) \_\_builtin\_trap(); else if (data[pick].bag\_id < bag\_id) min = pick + 1; else if (data[pick].bag\_id > bag\_id) max = pick - 1; } } static uint32\_t shiny\_gold\_hash; int is\_p1\_mark\_idx(int idx); int is\_p1\_mark(int id) { if (id == shiny\_gold\_hash) return 1; return is\_p1\_mark\_idx(find\_bag\_idx(id)); } int is\_p1\_mark\_idx(int idx) { if (data[idx].p1\_state == -1) { for (int i = 0; i < data[idx].contain\_cnt; i++) { if (is\_p1\_mark(data[idx].contain[i].bag\_id)) { return data[idx].p1\_state = 1; } } return data[idx].p1\_state = 0; } else return data[idx].p1\_state; } int count\_bag(uint32\_t id) { int idx = find\_bag\_idx(id); int acc = 1; for (int i = 0; i < data[idx].contain\_cnt; i++) { acc += count\_bag(data[idx].contain[i].bag\_id) \* data[idx].contain[i].bag\_cnt; } return acc; } void calc\_sgh() { char \*ptr = "shiny gold"; shiny\_gold\_hash = scan\_color(&ptr); } int main(int argc, char \*\*argv) { calc\_sgh(); // read input FILE \*fd = map\_file("test.txt"); char line[MAX\_LINE]; while (fgets(line, MAX\_LINE, fd) != NULL) { if (line[0] <= &\#39;\n&\#39;) continue; char \*ptr = line; uint32\_t head\_id = scan\_color(&ptr); ptr += 14; if (!strcmp(ptr, "no other bags.\n")) { data[data\_len++] = (struct bag) {.bag\_id = head\_id, .p1\_state = 0, .contain\_cnt = 0}; } else { struct bag \*cur\_bag = data + data\_len; \*cur\_bag = (struct bag) {.bag\_id = head\_id, .p1\_state = -1, .contain\_cnt = 0}; do { while (\*ptr < &\#39;0&\#39;) ptr++; struct item\_entry \*cur\_ent = cur\_bag->contain + (cur\_bag->contain\_cnt++); cur\_ent->bag\_cnt = 0; while (\*ptr != &\#39; &\#39;) { cur\_ent->bag\_cnt \*= 10; cur\_ent->bag\_cnt += \*ptr - &\#39;0&\#39;; ptr++; } ptr++; cur\_ent->bag\_id = scan\_color(&ptr); ptr += 4; if (\*ptr == &\#39;s&\#39;) ptr++; } while (\*ptr != &\#39;.&\#39;); data\_len++; } } int p1\_acc = 0; qsort(data, data\_len, sizeof(struct bag), &compare\_bag); for (int i = 0; i < data\_len; i++) { p1\_acc += is\_p1\_mark\_idx(i); } printf("P1: %d\n", (int) p1\_acc); printf("P2: %d\n", count\_bag(shiny\_gold\_hash) - 1); return 0; }
unknown-cnile/ucnile-day08.cunknown-cnile/ucnile-day08.c
\#include <stdlib.h> \#include <stdio.h> \#include <string.h> \#include <sys/types.h> \#include <sys/stat.h> \#include <fcntl.h> \#include <sys/mman.h> \#include <unistd.h> \#include <stdint.h> void die(char \*str) { fprintf(stderr, "[ERROR] %s\n", str); exit(-1); } FILE \*map\_file(const char \*filename) { int fd; if ((fd = open(filename, O\_RDONLY)) == -1) die("failed to open file"); struct stat fd\_stat; if (fstat(fd, &fd\_stat) == -1) die("failed to stat file"); void \*data; // never unmapped, probably fine if ((data = mmap(NULL, fd\_stat.st\_size, PROT\_READ, MAP\_PRIVATE, fd, 0)) == MAP\_FAILED) die("failed to map file"); close(fd); return fmemopen(data, fd\_stat.st\_size, "r"); } \#define MAX\_DATA 4096 \#define INS\_ACC 0 \#define INS\_JMP 1 \#define INS\_NOP 2 struct ins { uint32\_t op; uint32\_t val; }; struct ins asm\_data[MAX\_DATA]; int asm\_mark[MAX\_DATA]; int asm\_len = 0; \#define MAX\_LINE 256 int exec\_asm() { int pos = 0, acc = 0; while (1) { if ((pos > asm\_len) || (pos < 0)) return 0; if (pos == asm\_len) { printf("P2: %d\n", acc); exit(0); } else if (asm\_mark[pos]) { memset(asm\_mark, 0, sizeof(asm\_mark)); return acc; } asm\_mark[pos] = 1; switch (asm\_data[pos].op) { case INS\_ACC: acc += asm\_data[pos].val; pos++; break; case INS\_JMP: pos += asm\_data[pos].val; break; case INS\_NOP: pos++; break; } } } int main(int argc, char \*\*argv) { // read input FILE \*fd = map\_file("test.txt"); char line[MAX\_LINE]; while (fgets(line, MAX\_LINE, fd) != NULL) { char optxt[8]; int arg; if (sscanf(line, "%s %d", optxt, &arg) != 2) break; asm\_data[asm\_len].val = arg; switch (\*optxt) { case &\#39;\n&\#39;: case &\#39;\0&\#39;: break; case &\#39;a&\#39;: asm\_data[asm\_len++].op = INS\_ACC; break; case &\#39;j&\#39;: asm\_data[asm\_len++].op = INS\_JMP; break; case &\#39;n&\#39;: asm\_data[asm\_len++].op = INS\_NOP; break; } } printf("P1: %d\n", exec\_asm()); for (int i = 0; i < asm\_len; i++) { if (asm\_data[i].op == INS\_ACC) continue; asm\_data[i].op ^= 3; exec\_asm(); asm\_data[i].op ^= 3; } return 0; }
unknown-cnile/ucnile-day11.cunknown-cnile/ucnile-day11.c
\#include <stdlib.h> \#include <stdio.h> \#include <string.h> \#include <sys/types.h> \#include <sys/stat.h> \#include <fcntl.h> \#include <sys/mman.h> \#include <unistd.h> \#include <stdint.h> \#include <limits.h> void die(char \*str) { fprintf(stderr, "[ERROR] %s\n", str); exit(-1); } FILE \*map\_file(const char \*filename) { int fd; if ((fd = open(filename, O\_RDONLY)) == -1) die("failed to open file"); struct stat fd\_stat; if (fstat(fd, &fd\_stat) == -1) die("failed to stat file"); void \*data; // never unmapped, probably fine if ((data = mmap(NULL, fd\_stat.st\_size, PROT\_READ, MAP\_PRIVATE, fd, 0)) == MAP\_FAILED) die("failed to map file"); close(fd); return fmemopen(data, fd\_stat.st\_size, "r"); } \#define MAX\_X 128 \#define MAX\_Y 4096 uint8\_t data\_orig[MAX\_X\*MAX\_Y]; uint8\_t data\_1[MAX\_X\*MAX\_Y]; uint8\_t data\_2[MAX\_X\*MAX\_Y]; uint8\_t \*data\_cur = data\_orig; uint8\_t \*data\_nxt = data\_1; int row\_cnt = 0; \#define MAX\_LINE 512 \#define FLOOR 0 \#define EMPTY 1 \#define FULL 2 void restore() { data\_cur = data\_orig; data\_nxt = data\_1; } void swap() { data\_cur = (data\_cur == data\_1) ? data\_2 : data\_1; data\_nxt = (data\_nxt == data\_1) ? data\_2 : data\_1; } uint8\_t \*next\_seat(int x, int y, int dx, int dy, int is\_p1) { while (1) { x += dx; y += dy; if ((x < 0) || (x >= MAX\_X) || (y < 0) || (y >= row\_cnt)) return NULL; uint8\_t \*r = data\_cur + (y\*MAX\_X+x); if (\*r != FLOOR) return r; if (is\_p1) return NULL; } } int find\_adj(int is\_p1, int x, int y) { int acc = 0; for (int dx = -1; dx < 2; dx++) { for (int dy = -1; dy < 2; dy++) { if (dx | dy) { uint8\_t \*ptr = next\_seat(x, y, dx, dy, is\_p1); if (ptr && (\*ptr == FULL)) acc++; } } } return acc; } int tick(int is\_p1) { for (int y = 0; y < row\_cnt; y++) { for (int x = 0; x < MAX\_X; x++) { switch (data\_cur[y\*MAX\_X+x]) { case FLOOR: data\_nxt[y\*MAX\_X+x] = FLOOR; break; case EMPTY: data\_nxt[y\*MAX\_X+x] = find\_adj(is\_p1, x, y) ? EMPTY : FULL; break; case FULL: data\_nxt[y\*MAX\_X+x] = ((find\_adj(is\_p1, x, y) + is\_p1) >= 5) ? EMPTY : FULL; break; default: \_\_builtin\_trap(); } } } uint8\_t \*old\_nxt = data\_nxt; int r = !!memcmp(data\_cur, data\_nxt, row\_cnt\*MAX\_X); swap(); return r; } void prt() { for (int y = 0; y < row\_cnt; y++) { for (int x = 0; x < MAX\_X; x++) switch (data\_cur[y\*MAX\_X+x]) { case FLOOR: putchar(&\#39;.&\#39;); break; case EMPTY: putchar(&\#39;L&\#39;); break; case FULL: putchar(&\#39;@&\#39;); break; } putchar(&\#39;\n&\#39;); } } int cnt\_occ() { int acc = 0; for (int i = 0; i < MAX\_X\*row\_cnt; i++) { acc += data\_cur[i] == FULL; } return acc; } int main(int argc, char \*\*argv) { // read input FILE \*fd = map\_file("test.txt"); char line[MAX\_LINE]; while (fgets(line, MAX\_LINE, fd) != NULL) { for (int i = 0; line[i] > &\#39;\n&\#39;; i++) { if (i == MAX\_X) die("overflow"); uint8\_t v; switch (line[i]) { case &\#39;L&\#39;: v = EMPTY; break; case &\#39;\#&\#39;: v = FULL; break; default: v = FLOOR; break; } data\_orig[MAX\_X\*row\_cnt+i] = v; } row\_cnt++; } // part one while (tick(1)) {} printf("P1: %d\n", cnt\_occ()); restore(); // part two while (tick(0)) {} printf("P2: %d\n", cnt\_occ()); return 0; }
unknown-cnile/ucnile-day13.cunknown-cnile/ucnile-day13.c
\#include <stdlib.h> \#include <stdio.h> \#include <string.h> \#include <sys/stat.h> \#include <fcntl.h> \#include <sys/mman.h> \#include <unistd.h> \#include <complex.h> void die(char \*str) { fprintf(stderr, "[ERROR] %s\n", str); exit(-1); } FILE \*map\_file(const char \*filename) { int fd; if ((fd = open(filename, O\_RDONLY)) == -1) die("failed to open file"); struct stat fd\_stat; if (fstat(fd, &fd\_stat) == -1) die("failed to stat file"); void \*data; // never unmapped, probably fine if ((data = mmap(NULL, fd\_stat.st\_size, PROT\_READ, MAP\_PRIVATE, fd, 0)) == MAP\_FAILED) die("failed to map file"); close(fd); return fmemopen(data, fd\_stat.st\_size, "r"); } \#define MAX\_LINE 32 int main(int argc, char \*\*argv) { // read input FILE \*fd = map\_file("test.txt"); char line[MAX\_LINE]; \_Complex int p1\_s = 0; \_Complex int p1\_v = 1; \_Complex int p2\_s = 0; \_Complex int p2\_v = 10+1i; while (fgets(line, MAX\_LINE, fd) != NULL) { char c; unsigned int n; if (sscanf(line, "%c%u", &c, &n) != 2) break; // generates case for cardinal direction \#define PMSK(c, v) case c: p1\_s += n \* v; p2\_v += n \* v; break; // generates case for ccw angle change \#define RMSK(a, v) case a: p1\_v \*= v; p2\_v \*= v; break; // generates switch statements with trap on default \#define SW\_SAFE\_END default: \_\_builtin\_trap(); } \#define SW\_SAFE(sw, body) switch (sw) { body SW\_SAFE\_END switch (c) { PMSK(&\#39;N&\#39;, 1i) PMSK(&\#39;S&\#39;, -1i) PMSK(&\#39;E&\#39;, 1) PMSK(&\#39;W&\#39;, -1) case &\#39;R&\#39;: n = 360 - n; case &\#39;L&\#39;: SW\_SAFE(n, RMSK(90, 1i) RMSK(180, -1) RMSK(270, -1i)) break; case &\#39;F&\#39;: p1\_s += n \* p1\_v; p2\_s += n \* p2\_v; break; SW\_SAFE\_END } // part one printf("P1: %d\n", abs(\_\_real\_\_ p1\_s) + abs(\_\_imag\_\_ p1\_s)); // part two printf("P2: %d\n", abs(\_\_real\_\_ p2\_s) + abs(\_\_imag\_\_ p2\_s)); return 0; }