Compare commits

..

5 Commits

Author SHA1 Message Date
f895accae6 day 8 2025-12-08 16:02:47 -06:00
2994837ebc day 7 2025-12-08 16:02:36 -06:00
d6f3266154 prettier 2025-12-06 19:17:27 -06:00
75a8e72093 prettify 2025-12-06 15:55:16 -06:00
e71c742863 day 6 2025-12-06 14:29:30 -06:00
7 changed files with 1007 additions and 25 deletions

View File

@@ -14,13 +14,13 @@ LFLAGS = -Ttext=C475000 -static -nostdlib -no-pie
all: $(BINS)
%.elf: %.o start.o | %.c lib.h $(MUSLOBJS)
%.elf: %.o start.o | %.c $(MUSLOBJS)
ld -o $@ $(LFLAGS) start.o $< $(MUSLOBJS)
start.o: start.s
nasm -f elf64 -o $@ $<
%.o: %.c
%.o: %.c | lib.h
gcc-tree -o $@ $(CFLAGS) $<
clean:

45
day0.h
View File

@@ -3,46 +3,45 @@
#define DBG 0
static
unsigned char input[] = {
ch input[] = {
#embed "day0_input.txt"
};
static
unsigned char test[] = {
ch test[] = {
#embed "day0_test.txt"
};
alignas(0x40) static
struct {
line padstart[4];
struct { ch *s; num _extra[7]; };
union { line l; struct { ch s[16]; num n; }; } result;
line padend;
} m;
static_assert(offsetof(typeof(m),result) % 64 == 0);
static_assert(offsetof(typeof(m),padend) % 64 == 0);
static
unsigned long do_part1(size_t file_len, unsigned char file[file_len]) {
unsigned char *s = file;
unsigned long result = 0;
num do_part1(size_t file_len, ch file[file_len]) {
m = (typeof(m)){ };
m.s = file;
m.result.n = 0;
#if DBG
print("\n");
#endif
do {
s++;
} while (s != &file[file_len]);
m.s++;
} while (m.s != &file[file_len]);
return result;
return m.result.n;
}
static
unsigned long do_part2(size_t file_len, unsigned char file[file_len]) {
unsigned char *s = file;
unsigned long result = 0;
#if DBG
print("\n");
#endif
do {
s++;
} while (s != &file[file_len]);
return result;
num do_part2(size_t file_len, ch file[file_len]) {
return 0;
}
#define RUN_TEST1 1
@@ -56,7 +55,7 @@ do {
void run() {
#if RUN_TEST1
print("PART 1 TEST: ");
if (unsigned long v = do_part1(countof(test), test); v != TEST1_EXPECT) {
if (num v = do_part1(countof(test), test); v != TEST1_EXPECT) {
print("FAILED (got ");
printd(v);
print(", expected " xstr(TEST1_EXPECT) ")\n");
@@ -73,7 +72,7 @@ void run() {
#if RUN_TEST2
print("PART 2 TEST: ");
if (unsigned long v = do_part2(countof(test), test); v != TEST2_EXPECT) {
if (num v = do_part2(countof(test), test); v != TEST2_EXPECT) {
print("FAILED (got ");
printd(v);
print(", expected " xstr(TEST2_EXPECT) ")\n");

228
day6.c Normal file
View File

@@ -0,0 +1,228 @@
#include "lib.h"
#define DBG 0
static
ch input[] = {
#embed "day6_input.txt"
};
static
ch test[] = {
#embed "day6_test.txt"
};
static
num do_part1(size_t file_len, ch file[file_len]) {
ch *s = file;
num result = 0;
s = memchr(s, '\n', file_len);
num stride = s - file;
s = file;
while (*s != '+' && *s != '*') s += stride;
num heightb = (s - file);
num height = (s - file) / stride;
ch *op = s;
// constructing a pointer more than 1 past the end of an array
// is UB, sadly, so we'll use offsets
num op_off = op - file;
s = file;
do {
num ledge_off = op_off - heightb;
if (s[op_off] == '+') {
num a = 0;
num rem = height;
do {
ch *t = s + ledge_off;
while (*t < '0') t++;
num b = grabnum(t, NULL);
a += b;
ledge_off += stride;
rem--;
} while (rem);
result += a;
} else if (s[op_off] == '*') {
num a = 1;
num rem = height;
do {
ch *t = s + ledge_off;
while (*t < '0') t++;
num b = grabnum(t, NULL);
#if DBG
if (rem < height) { print("\t*\t"); }
printd(b);
#endif
a *= b;
ledge_off += stride;
rem--;
} while (rem);
result += a;
#if DBG
print("\t= "); printd(a); print("\n");
#endif
}
#if DBG
else {
print("INVALID OP\n");
exit_group(-1);
}
#endif
op_off++;
while (op_off < file_len) {
if (s[op_off] == '+' || s[op_off] == '*')
break;
op_off++;
}
} while (op_off < file_len);
return result;
}
static
num do_part2(size_t file_len, unsigned char file[file_len]) {
ch *s = file;
num result = 0;
#if DBG
print("\n");
#endif
s = memchr(s, '\n', file_len);
num stride = s - file;
s = file;
while (*s != '+' && *s != '*') s += stride;
num heightb = (s - file);
num height = (s - file) / stride;
ch *op = s;
#if DBG
print("STRIDE\t"); printd(stride); print("\n");
print("HEIGHT\t"); printd(height); print("\n");
#endif
// constructing a pointer more than 1 past the end of an array
// is UB, sadly, so we'll use offsets
num fop_off = op - file;
num prev_op_off = fop_off + stride - 1;
s = file;
do {
// we don't need to do these in the same order as the example
// because both these operations are commutative.
// but you know what? we can anyway. for fun. and pretty output
num op_off = prev_op_off - 1;
while (s[op_off] != '+' && s[op_off] != '*' && s[op_off] != '\n')
op_off--;
num ledge_off = op_off - heightb;
num redge_off = (s[prev_op_off] == '\n' ? -1 : -2) + prev_op_off - heightb;
if (s[op_off] == '+') {
num a = 0;
num rem = redge_off - ledge_off + 1;
num frem = rem;
do {
num b = 0;
ch *t = s + redge_off;
for (num i = height; i; i--,t+=stride) {
if (*t >= '0') {
b *= 10;
b += *t - '0';
}
}
#if DBG
if (rem < frem) { print("\t+\t"); }
printd(b);
#endif
a += b;
redge_off--;
rem--;
} while (rem);
result += a;
#if DBG
print("\t=\t"); printd(a); print("\n");
#endif
} else if (s[op_off] == '*') {
num a = 1;
num rem = redge_off - ledge_off + 1;
num frem = rem;
do {
num b = 0;
ch *t = s + redge_off;
for (num i = height; i; i--,t+=stride) {
if (*t >= '0') {
b *= 10;
b += *t - '0';
}
}
#if DBG
if (rem < frem) { print("\t*\t"); }
printd(b);
#endif
a *= b;
redge_off--;
rem--;
} while (rem);
result += a;
#if DBG
print("\t=\t"); printd(a); print("\n");
#endif
}
#if DBG
else {
print("INVALID OP\n");
exit_group(-1);
}
#endif
prev_op_off = op_off;
} while (prev_op_off != fop_off);
return result;
}
#define RUN_TEST1 1
#define RUN_PART1 1
#define RUN_TEST2 1
#define RUN_PART2 1
#define TEST1_EXPECT 4277556
#define TEST2_EXPECT 3263827
void run() {
#if RUN_TEST1
print("PART 1 TEST: ");
if (num v = do_part1(countof(test), test); v != TEST1_EXPECT) {
print("FAILED (got ");
printd(v);
print(", expected " xstr(TEST1_EXPECT) ")\n");
} else {
print("PASSED\n");
}
#endif
#if RUN_PART1
print("PART 1 RESULT: ");
printd(do_part1(countof(input), input));
print("\n");
#endif
#if RUN_TEST2
print("PART 2 TEST: ");
if (num v = do_part2(countof(test), test); v != TEST2_EXPECT) {
print("FAILED (got ");
printd(v);
print(", expected " xstr(TEST2_EXPECT) ")\n");
} else {
print("PASSED\n");
}
#endif
#if RUN_PART2
print("PART 2 RESULT: ");
printd(do_part2(countof(input), input));
print("\n");
#endif
exit_group(0);
}

228
day6pretty.c Normal file
View File

@@ -0,0 +1,228 @@
#include "lib.h"
static
ch input[] = {
#embed "day6_input.txt"
};
static
ch test[] = {
#embed "day6_test.txt"
};
typedef struct { ch l[64]; } line;
static_assert(sizeof(line) == 0x40);
alignas(0x40) static
struct {
line padstart[4];
struct { num pad[3]; num prev_op_off, op_off, ledge_off, redge_off, rem; };
union { line l; struct { ch s[16]; num op, pad0, pad1, v; }; } b;
union { line l; struct { ch s[16], pad0, sr[16], pad1; num v, vr; }; } a;
line padend;
} m;
static_assert(offsetof(typeof(m),b) % 64 == 0);
static_assert(sizeof(m.b) == 64);
static_assert(sizeof(m.a) == 64);
static_assert(offsetof(typeof(m),padend) % 64 == 0);
static
num do_part1(size_t file_len, ch file[file_len]) {
ch *s = file;
m = (typeof(m)){ };
s = memchr(s, '\n', file_len);
const num stride = s - file;
s = file;
while (*s != '+' && *s != '*') s += stride;
const num heightb = (s - file);
const num height = (s - file) / stride;
ch *const op = s;
// constructing a pointer more than 1 past the end of an array
// is UB, sadly, so we'll use offsets
m.op_off = op - file;
s = file;
do {
m.ledge_off = m.op_off - heightb;
if (s[m.op_off] == '+') {
m.a.v = 0;
m.rem = height;
m.b.op = '+' << 8;
snprintd(m.a.v, 16, m.a.s);
do {
ch *t = s + m.ledge_off;
while (*t < '0') t++;
m.b.v = grabnum(t, NULL);
snprintd(m.b.v, 16, m.b.s);
m.a.v += m.b.v;
snprintd(m.a.v, 16, m.a.s);
m.ledge_off += stride;
m.rem--;
} while (m.rem);
m.a.vr += m.a.v;
snprintd(m.a.vr, 16, m.a.sr);
} else if (s[m.op_off] == '*') {
m.a.v = 1;
m.rem = height;
m.b.op = '*' << 8;
snprintd(m.a.v, 16, m.a.s);
do {
ch *t = s + m.ledge_off;
while (*t < '0') t++;
m.b.v = grabnum(t, NULL);
snprintd(m.b.v, 16, m.b.s);
m.a.v *= m.b.v;
snprintd(m.a.v, 16, m.a.s);
m.ledge_off += stride;
m.rem--;
} while (m.rem);
m.a.vr += m.a.v;
snprintd(m.a.vr, 16, m.a.sr);
}
m.op_off++;
while (m.op_off < file_len) {
if (s[m.op_off] == '+' || s[m.op_off] == '*')
break;
m.op_off++;
}
} while (m.op_off < file_len);
return m.a.vr;
}
static
num do_part2(size_t file_len, unsigned char file[file_len]) {
ch *s = file;
m = (typeof(m)){ };
s = memchr(s, '\n', file_len);
const num stride = s - file;
s = file;
while (*s != '+' && *s != '*') s += stride;
const num heightb = (s - file);
const num height = (s - file) / stride;
ch *const op = s;
// constructing a pointer more than 1 past the end of an array
// is UB, sadly, so we'll use offsets
const num fop_off = op - file;
m.prev_op_off = fop_off + stride - 1;
s = file;
do {
// we don't need to do these in the same order as the example
// because both these operations are commutative.
// but you know what? we can anyway. for fun. and pretty output
{
num op_off = m.prev_op_off - 1;
while (s[op_off] != '+' && s[op_off] != '*' && s[op_off] != '\n')
op_off--;
m.op_off = op_off;
}
m.ledge_off = m.op_off - heightb;
m.redge_off = (s[m.prev_op_off] == '\n' ? -1 : -2) + m.prev_op_off - heightb;
if (s[m.op_off] == '+') {
m.a.v = 0;
m.rem = m.redge_off - m.ledge_off + 1;
const num frem = m.rem;
m.b.op = '+' << 8;
snprintd(m.a.v, 16, m.a.s);
do {
m.b.v = 0;
ch *t = s + m.redge_off;
for (num i = height; i; i--,t+=stride) {
if (*t >= '0') {
m.b.v *= 10;
m.b.v += *t - '0';
}
}
snprintd(m.b.v, 16, m.b.s);
m.a.v += m.b.v;
snprintd(m.a.v, 16, m.a.s);
m.redge_off--;
m.rem--;
} while (m.rem);
m.a.vr += m.a.v;
snprintd(m.a.vr, 16, m.a.sr);
} else if (s[m.op_off] == '*') {
m.a.v = 1;
m.rem = m.redge_off - m.ledge_off + 1;
const num frem = m.rem;
m.b.op = '*' << 8;
snprintd(m.a.v, 16, m.a.s);
do {
m.b.v = 0;
ch *t = s + m.redge_off;
for (num i = height; i; i--,t+=stride) {
if (*t >= '0') {
m.b.v *= 10;
m.b.v += *t - '0';
}
}
snprintd(m.b.v, 16, m.b.s);
m.a.v *= m.b.v;
snprintd(m.a.v, 16, m.a.s);
m.redge_off--;
m.rem--;
} while (m.rem);
m.a.vr += m.a.v;
snprintd(m.a.vr, 16, m.a.sr);
}
m.prev_op_off = m.op_off;
} while (m.prev_op_off != fop_off);
return m.a.vr;
}
#define RUN_TEST1 1
#define RUN_PART1 1
#define RUN_TEST2 1
#define RUN_PART2 1
#define TEST1_EXPECT 4277556
#define TEST2_EXPECT 3263827
void run() {
#if RUN_TEST1
print("PART 1 TEST: ");
if (num v = do_part1(countof(test), test); v != TEST1_EXPECT) {
print("FAILED (got ");
printd(v);
print(", expected " xstr(TEST1_EXPECT) ")\n");
} else {
print("PASSED\n");
}
#endif
#if RUN_PART1
print("PART 1 RESULT: ");
printd(do_part1(countof(input), input));
print("\n");
#endif
#if RUN_TEST2
print("PART 2 TEST: ");
if (num v = do_part2(countof(test), test); v != TEST2_EXPECT) {
print("FAILED (got ");
printd(v);
print(", expected " xstr(TEST2_EXPECT) ")\n");
} else {
print("PASSED\n");
}
#endif
#if RUN_PART2
print("PART 2 RESULT: ");
printd(do_part2(countof(input), input));
print("\n");
#endif
exit_group(0);
}

178
day7.c Normal file
View File

@@ -0,0 +1,178 @@
#include "lib.h"
#define DBG 0
static
ch input[32768] = {
#embed "day7_input.txt"
};
static
ch test[512] = {
#embed "day7_test.txt"
};
typedef struct { ch l[64]; } line;
static_assert(sizeof(line) == 0x40);
alignas(0x40) static
struct {
line padstart[4];
num file_stride;
num stride;
num _extra[6];
union { line l; struct { ch s[16]; num n; }; } result;
line padend;
} m;
static_assert(offsetof(typeof(m),result) % 64 == 0);
static_assert(offsetof(typeof(m),padend) % 64 == 0);
alignas(4096) ch buf1[256];
alignas(4096) num buf[256];
static
num do_part1(size_t file_len, ch file[file_len]) {
m = (typeof(m)){ };
m.result.n = 0;
#if DBG
pnl();
#endif
memset(buf1, 0, sizeof(buf1));
m.file_stride = findc(file, '\n', file_len) - file;
num linecnt = (findc(file, 0, file_len) - file) / m.file_stride;
#if DBG
print("stride "); printd(m.file_stride); pnl();
print("lines "); printd(linecnt); pnl();
#endif
num s_pos = findc(file, 'S', - 1) - 1 - file;
buf1[s_pos] = '|';
num line = 2;
do {
ch *l = file + line * m.file_stride;
for (num col = 0; col < m.file_stride - 1; col++) {
ch c = l[col];
if (c == '^') {
m.result.n += buf1[col] > 0;
buf1[col-1] |= buf1[col];
buf1[col+1] |= buf1[col];
buf1[col] = 0;
}
}
#if DBG
for (num col = 0; col < m.file_stride; col++) {
if (buf1[col] == '|') print("|");
else if (l[col] == '^') print("^");
else print(" ");
}
pnl();
#endif
line+=2;
} while (line < linecnt);
return m.result.n;
}
static
num do_part2(size_t file_len, ch file[file_len]) {
m = (typeof(m)){ };
#if DBG
pnl();
#endif
memset(buf, 0, sizeof(buf));
m.file_stride = findc(file, '\n', file_len) - file;
num linecnt = (findc(file, 0, file_len) - file) / m.file_stride;
#if DBG
print("stride "); printd(m.file_stride); pnl();
print("lines "); printd(linecnt); pnl();
#endif
num s_pos = findc(file, 'S', - 1) - 1 - file;
buf[s_pos] = 1;
num line = 2;
do {
ch *l = file + line * m.file_stride;
for (num col = 0; col < m.file_stride - 1; col++) {
ch c = l[col];
if (c == '^') {
buf[col-1] += buf[col];
buf[col+1] += buf[col];
buf[col] = 0;
}
}
for (num col = 0; col < m.file_stride; col++) {
num v = buf[col];
#if DBG
if (l[col] == '^') print(" ");
else if (v >> 32) print("");
else if (v >> 16) print("");
else if (v >> 8) print("");
else if (v >> 4) print("");
else if (v >> 2) print("-");
else if (v >> 0) print(".");
else print(" ");
#endif
}
#if DBG
pnl();
#endif
line+=2;
} while (line < linecnt);
m.result.n = 0;
for (num col = 0; col < m.file_stride; col++) {
m.result.n += buf[col];
}
return m.result.n;
}
#define RUN_TEST1 1
#define RUN_PART1 1
#define RUN_TEST2 1
#define RUN_PART2 1
#define TEST1_EXPECT 21
#define TEST2_EXPECT 40
void run() {
#if RUN_TEST1
print("PART 1 TEST: ");
if (num v = do_part1(countof(test), test); v != TEST1_EXPECT) {
print("FAILED (got ");
printd(v);
print(", expected " xstr(TEST1_EXPECT) ")\n");
} else {
print("PASSED\n");
}
#endif
#if RUN_PART1
print("PART 1 RESULT: ");
printd(do_part1(countof(input), input));
print("\n");
#endif
#if RUN_TEST2
print("PART 2 TEST: ");
if (num v = do_part2(countof(test), test); v != TEST2_EXPECT) {
print("FAILED (got ");
printd(v);
print(", expected " xstr(TEST2_EXPECT) ")\n");
} else {
print("PASSED\n");
}
#endif
#if RUN_PART2
print("PART 2 RESULT: ");
printd(do_part2(countof(input), input));
print("\n");
#endif
exit_group(0);
}

317
day8.c Normal file
View File

@@ -0,0 +1,317 @@
#include "lib.h"
#include <stdlib.h>
#define DBG 0
static
ch input[] = {
#embed "day8_input.txt"
};
static
ch test[] = {
#embed "day8_test.txt"
};
typedef num point __attribute__((vector_size(4*sizeof(num))));
alignas(64) static
point positions[1000];
alignas(64) static
unsigned short circuits[1000];
alignas(1024) static
struct { num min_dist; unsigned short min_target; } targets[1000][1024];
alignas(64) static
unsigned short next_targets[1000];
alignas(64) static
num next_dists[1000];
alignas(64) static
unsigned short next_indices[1000];
static
int targetcompar(const void *a, const void *b) {
const num ad = ((typeof(**targets)*)a)->min_dist;
const num bd = ((typeof(**targets)*)b)->min_dist;
if (ad > bd) return 1;
else if (ad < bd) return -1;
else return 0;
}
static
num do_part1(size_t file_len, ch file[file_len], bool istest) {
const num iters = istest ? 10 : 1000;
const num pointcount = istest ? 20 : 1000;
num iter = 0;
#if DBG
pnl();
#endif
// read in positions from file
{ ch *s = file;
for (num i = 0; i < pointcount; i++) {
unsigned x = grabnum(s, &s);
s++;
unsigned y = grabnum(s, &s);
s++;
unsigned z = grabnum(s, &s);
s++;
positions[i] = (point){ x,y,z };
} }
// set up initial circuits
for (num i = 0; i < pointcount; i += 4) {
circuits[i+0] = i+0;
circuits[i+1] = i+1;
circuits[i+2] = i+2;
circuits[i+3] = i+3;
}
// find distances between all points
for (num i = 0; i < pointcount; i++) {
point pi = positions[i];
for (num j = 0; j < pointcount; j += 4) {
point pj0 = positions[j+0];
point pj1 = positions[j+1];
point pj2 = positions[j+2];
point pj3 = positions[j+3];
point dvp0 = (pi - pj0) * (pi - pj0);
point dvp1 = (pi - pj1) * (pi - pj1);
point dvp2 = (pi - pj2) * (pi - pj2);
point dvp3 = (pi - pj3) * (pi - pj3);
num d0 = dvp0[0] + dvp0[1] + dvp0[2] + dvp0[3];
num d1 = dvp1[0] + dvp1[1] + dvp1[2] + dvp1[3];
num d2 = dvp2[0] + dvp2[1] + dvp2[2] + dvp2[3];
num d3 = dvp3[0] + dvp3[1] + dvp3[2] + dvp3[3];
targets[i][j+0] = (typeof(**targets)) { d0, j+0 };
targets[i][j+1] = (typeof(**targets)) { d1, j+1 };
targets[i][j+2] = (typeof(**targets)) { d2, j+2 };
targets[i][j+3] = (typeof(**targets)) { d3, j+3 };
}
// sort targets. slow.
qsort(targets[i], pointcount, sizeof(**targets), targetcompar);
}
// set up initial next targets
for (num i = 0; i < pointcount; i++) {
next_targets[i] = targets[i][1].min_target;
next_dists[i] = targets[i][1].min_dist;
next_indices[i] = 1;
}
// FIXED JOIN ITERATIONS
do {
// find min dist
num min_dist = -1;
num min_i = 0;
for (num i = 0; i < pointcount; i++)
if (next_dists[i] < min_dist)
min_dist = next_dists[min_i = i];
num min_j = next_targets[min_i];
// -- join the pair --
#if DBG
point ipos = positions[min_i];
point jpos = positions[min_j];
printd(ipos[0]); print(","); printd(ipos[1]); print(","); printd(ipos[2]);
print("\t->\t");
printd(jpos[0]); print(","); printd(jpos[1]); print(","); printd(jpos[2]);
pnl();
#endif
// update circuits
unsigned old_circuit = circuits[min_j];
unsigned new_circuit = circuits[min_i];
if (old_circuit != new_circuit) {
for (num c = 0; c < pointcount; c++)
if (circuits[c] == old_circuit)
circuits[c] = new_circuit;
}
// update targets
unsigned niidx = ++next_indices[min_i];
unsigned njidx = ++next_indices[min_j];
next_targets[min_i] = targets[min_i][niidx].min_target;
next_targets[min_j] = targets[min_j][njidx].min_target;
next_dists[min_i] = targets[min_i][niidx].min_dist;
next_dists[min_j] = targets[min_j][njidx].min_dist;
iter++;
} while(iter < iters);
// reuse next_targets to store circuit counts
for (num i = 0; i < pointcount; i++) {
next_targets[i] = 0;
}
for (num i = 0; i < pointcount; i++) {
next_targets[circuits[i]]++;
}
num circ0 = 0, circ1 = 0, circ2 = 0;
for (num i = 0; i < pointcount; i++) {
if (next_targets[i] > circ0) {
circ2 = circ1;
circ1 = circ0;
circ0 = next_targets[i];
} else if (next_targets[i] > circ1) {
circ2 = circ1;
circ1 = next_targets[i];
} else if (next_targets[i] > circ2) {
circ2 = next_targets[i];
}
}
return circ0 * circ1 * circ2;
}
static
num do_part2(size_t file_len, ch file[file_len], bool istest) {
const num pointcount = istest ? 20 : 1000;
#if DBG
pnl();
#endif
// read in positions from file
{ ch *s = file;
for (num i = 0; i < pointcount; i++) {
unsigned x = grabnum(s, &s);
s++;
unsigned y = grabnum(s, &s);
s++;
unsigned z = grabnum(s, &s);
s++;
positions[i] = (point){ x,y,z };
} }
// set up initial circuits
for (num i = 0; i < pointcount; i += 4) {
circuits[i+0] = i+0;
circuits[i+1] = i+1;
circuits[i+2] = i+2;
circuits[i+3] = i+3;
}
// find distances between all points
for (num i = 0; i < pointcount; i++) {
point pi = positions[i];
for (num j = 0; j < pointcount; j += 4) {
point pj0 = positions[j+0];
point pj1 = positions[j+1];
point pj2 = positions[j+2];
point pj3 = positions[j+3];
point dvp0 = (pi - pj0) * (pi - pj0);
point dvp1 = (pi - pj1) * (pi - pj1);
point dvp2 = (pi - pj2) * (pi - pj2);
point dvp3 = (pi - pj3) * (pi - pj3);
num d0 = dvp0[0] + dvp0[1] + dvp0[2] + dvp0[3];
num d1 = dvp1[0] + dvp1[1] + dvp1[2] + dvp1[3];
num d2 = dvp2[0] + dvp2[1] + dvp2[2] + dvp2[3];
num d3 = dvp3[0] + dvp3[1] + dvp3[2] + dvp3[3];
targets[i][j+0] = (typeof(**targets)) { d0, j+0 };
targets[i][j+1] = (typeof(**targets)) { d1, j+1 };
targets[i][j+2] = (typeof(**targets)) { d2, j+2 };
targets[i][j+3] = (typeof(**targets)) { d3, j+3 };
}
// sort targets. slow.
qsort(targets[i], pointcount, sizeof(**targets), targetcompar);
}
// set up initial next targets
for (num i = 0; i < pointcount; i++) {
next_targets[i] = targets[i][1].min_target;
next_dists[i] = targets[i][1].min_dist;
next_indices[i] = 1;
}
unsigned last_i = 0, last_j = 0;
// main iterations
do {
// find min dist
num min_dist = -1;
num min_i = 0;
for (num i = 0; i < pointcount; i++)
if (next_dists[i] < min_dist)
min_dist = next_dists[min_i = i];
num min_j = next_targets[min_i];
last_i = min_i;
last_j = min_j;
// -- join the pair --
#if DBG
point ipos = positions[min_i];
point jpos = positions[min_j];
printd(ipos[0]); print(","); printd(ipos[1]); print(","); printd(ipos[2]);
print("\t->\t");
printd(jpos[0]); print(","); printd(jpos[1]); print(","); printd(jpos[2]);
pnl();
#endif
// update circuits
unsigned old_circuit = circuits[min_j];
unsigned new_circuit = circuits[min_i];
if (old_circuit != new_circuit) {
bool done = true;
for (num c = 0; c < pointcount; c++) {
if (circuits[c] == old_circuit)
circuits[c] = new_circuit;
if (circuits[c] != new_circuit)
done = false;
}
if (done) break;
}
// update targets
unsigned niidx = ++next_indices[min_i];
unsigned njidx = ++next_indices[min_j];
next_targets[min_i] = targets[min_i][niidx].min_target;
next_targets[min_j] = targets[min_j][njidx].min_target;
next_dists[min_i] = targets[min_i][niidx].min_dist;
next_dists[min_j] = targets[min_j][njidx].min_dist;
} while(true);
return positions[last_i][0] * positions[last_j][0];
}
#define RUN_TEST1 1
#define RUN_PART1 1
#define RUN_TEST2 1
#define RUN_PART2 1
#define TEST1_EXPECT 40
#define TEST2_EXPECT 25272
void run() {
#if RUN_TEST1
print("PART 1 TEST: ");
if (num v = do_part1(countof(test), test, true); v != TEST1_EXPECT) {
print("FAILED (got ");
printd(v);
print(", expected " xstr(TEST1_EXPECT) ")\n");
} else {
print("PASSED\n");
}
#endif
#if RUN_PART1
print("PART 1 RESULT: ");
printd(do_part1(countof(input), input, false));
print("\n");
#endif
#if RUN_TEST2
print("PART 2 TEST: ");
if (num v = do_part2(countof(test), test, true); v != TEST2_EXPECT) {
print("FAILED (got ");
printd(v);
print(", expected " xstr(TEST2_EXPECT) ")\n");
} else {
print("PASSED\n");
}
#endif
#if RUN_PART2
print("PART 2 RESULT: ");
printd(do_part2(countof(input), input, false));
print("\n");
#endif
exit_group(0);
}

32
lib.h
View File

@@ -35,6 +35,37 @@ void exit_group(uint64_t code) {
typedef uint64_t bcdint;
typedef uint64_t num;
typedef unsigned char ch;
__attribute__((always_inline)) inline
ch *findc(const ch *s, ch c, size_t n) {
uint64_t dispose;
void *d;
asm("repnz scasb":"=D"(d),"=c"(dispose):"D"(s),"c"(n),"a"(c):"memory","cc");
return d;
}
__attribute__((always_inline)) inline
num *findnum(const num *s, num c, size_t n) {
uint64_t dispose;
void *d;
asm("repnz scasq":"=D"(d),"=c"(dispose):"D"(s),"c"(n),"a"(c):"memory","cc");
return d;
}
__attribute__((always_inline)) inline
num *findnotnum(const num *s, num c, size_t n) {
uint64_t dispose;
void *d;
asm("repz scasq":"=D"(d),"=c"(dispose):"D"(s),"c"(n),"a"(c):"memory","cc");
return d;
}
static inline num alignnum(num v, num align) {
num extra = v % align;
num adjust = (align - extra) % align;
return v + adjust;
}
static
void printns(size_t len, unsigned char chars[len]) {
@@ -63,6 +94,7 @@ void print_many_char(size_t len, char chars[len]) {
}
#define print(string) print_many_char(sizeof(string)-1, string)
#define pnl() print("\n")
static
void printd(unsigned long v) {