Compare commits
2 Commits
d6f3266154
...
f895accae6
| Author | SHA1 | Date | |
|---|---|---|---|
| f895accae6 | |||
| 2994837ebc |
23
day0.h
23
day0.h
@@ -12,27 +12,36 @@ 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
|
||||
num do_part1(size_t file_len, ch file[file_len]) {
|
||||
ch *s = file;
|
||||
num result = 0;
|
||||
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
|
||||
num do_part2(size_t file_len, ch file[file_len]) {
|
||||
num result = 0;
|
||||
return result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define RUN_TEST1 1
|
||||
|
||||
178
day7.c
Normal file
178
day7.c
Normal 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
317
day8.c
Normal 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);
|
||||
}
|
||||
39
lib.h
39
lib.h
@@ -9,14 +9,6 @@
|
||||
|
||||
#define STDOUT_FILENO 1
|
||||
|
||||
__attribute__((always_inline))
|
||||
void *memchr(const void *s, int 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;
|
||||
}
|
||||
|
||||
static
|
||||
void write(uint64_t fileno, void *buffer, size_t len) {
|
||||
uint64_t a,d;
|
||||
@@ -45,6 +37,36 @@ 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]) {
|
||||
write(STDOUT_FILENO, chars, len);
|
||||
@@ -72,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) {
|
||||
|
||||
Reference in New Issue
Block a user