~cdv/aoc-2019-c

cdb50ab60e56ad178351bf9dce22c2cf31a7c4b3 — Chris Vittal 3 months ago e66485a master
Solve day 23
2 files changed, 177 insertions(+), 3 deletions(-)

A input/day_23.txt
M solutions/day23.c
A input/day_23.txt => input/day_23.txt +1 -0
@@ 0,0 1,1 @@
3,62,1001,62,11,10,109,2219,105,1,0,1618,1375,2066,1202,1967,934,1295,771,1029,1099,1480,1406,1264,1690,866,2130,1235,1134,1330,903,802,1783,612,1874,1659,1171,1066,1996,2159,2031,705,1447,2097,2188,1934,736,1517,998,676,1587,1814,571,969,643,833,1719,1905,1845,1752,1554,0,0,0,0,0,0,0,0,0,0,0,0,3,64,1008,64,-1,62,1006,62,88,1006,61,170,1105,1,73,3,65,21001,64,0,1,20102,1,66,2,21102,105,1,0,1105,1,436,1201,1,-1,64,1007,64,0,62,1005,62,73,7,64,67,62,1006,62,73,1002,64,2,133,1,133,68,133,101,0,0,62,1001,133,1,140,8,0,65,63,2,63,62,62,1005,62,73,1002,64,2,161,1,161,68,161,1101,1,0,0,1001,161,1,169,102,1,65,0,1101,1,0,61,1102,0,1,63,7,63,67,62,1006,62,203,1002,63,2,194,1,68,194,194,1006,0,73,1001,63,1,63,1105,1,178,21102,210,1,0,106,0,69,2102,1,1,70,1101,0,0,63,7,63,71,62,1006,62,250,1002,63,2,234,1,72,234,234,4,0,101,1,234,240,4,0,4,70,1001,63,1,63,1105,1,218,1105,1,73,109,4,21101,0,0,-3,21101,0,0,-2,20207,-2,67,-1,1206,-1,293,1202,-2,2,283,101,1,283,283,1,68,283,283,22001,0,-3,-3,21201,-2,1,-2,1105,1,263,21202,-3,1,-3,109,-4,2106,0,0,109,4,21102,1,1,-3,21101,0,0,-2,20207,-2,67,-1,1206,-1,342,1202,-2,2,332,101,1,332,332,1,68,332,332,22002,0,-3,-3,21201,-2,1,-2,1106,0,312,22102,1,-3,-3,109,-4,2105,1,0,109,1,101,1,68,359,20102,1,0,1,101,3,68,367,20101,0,0,2,21102,376,1,0,1106,0,436,22101,0,1,0,109,-1,2106,0,0,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,2147483648,4294967296,8589934592,17179869184,34359738368,68719476736,137438953472,274877906944,549755813888,1099511627776,2199023255552,4398046511104,8796093022208,17592186044416,35184372088832,70368744177664,140737488355328,281474976710656,562949953421312,1125899906842624,109,8,21202,-6,10,-5,22207,-7,-5,-5,1205,-5,521,21101,0,0,-4,21101,0,0,-3,21101,0,51,-2,21201,-2,-1,-2,1201,-2,385,470,21001,0,0,-1,21202,-3,2,-3,22207,-7,-1,-5,1205,-5,496,21201,-3,1,-3,22102,-1,-1,-5,22201,-7,-5,-7,22207,-3,-6,-5,1205,-5,515,22102,-1,-6,-5,22201,-3,-5,-3,22201,-1,-4,-4,1205,-2,461,1106,0,547,21101,-1,0,-4,21202,-6,-1,-6,21207,-7,0,-5,1205,-5,547,22201,-7,-6,-7,21201,-4,1,-4,1106,0,529,21202,-4,1,-7,109,-8,2106,0,0,109,1,101,1,68,564,20102,1,0,0,109,-1,2106,0,0,1101,0,91961,66,1101,6,0,67,1101,598,0,68,1101,302,0,69,1102,1,1,71,1101,0,610,72,1105,1,73,0,0,0,0,0,0,0,0,0,0,0,0,36,53902,1101,82891,0,66,1102,1,1,67,1101,0,639,68,1101,556,0,69,1102,1,1,71,1102,1,641,72,1106,0,73,1,160,11,236204,1102,66491,1,66,1101,2,0,67,1102,670,1,68,1102,351,1,69,1102,1,1,71,1101,674,0,72,1106,0,73,0,0,0,0,255,57557,1101,0,60661,66,1102,1,1,67,1102,1,703,68,1101,0,556,69,1101,0,0,71,1101,0,705,72,1106,0,73,1,1391,1102,76159,1,66,1102,1,1,67,1102,732,1,68,1102,1,556,69,1101,1,0,71,1101,734,0,72,1106,0,73,1,25,26,173378,1102,1,73259,66,1102,1,3,67,1102,763,1,68,1102,302,1,69,1102,1,1,71,1101,0,769,72,1105,1,73,0,0,0,0,0,0,8,168676,1102,51481,1,66,1101,1,0,67,1102,1,798,68,1101,556,0,69,1101,1,0,71,1101,0,800,72,1105,1,73,1,23,41,275883,1101,0,35851,66,1101,0,1,67,1102,1,829,68,1102,556,1,69,1102,1,1,71,1101,831,0,72,1105,1,73,1,-3,9,30777,1102,69557,1,66,1102,2,1,67,1101,860,0,68,1102,1,302,69,1101,1,0,71,1102,864,1,72,1106,0,73,0,0,0,0,9,20518,1101,69697,0,66,1102,4,1,67,1101,0,893,68,1102,1,302,69,1102,1,1,71,1102,901,1,72,1106,0,73,0,0,0,0,0,0,0,0,27,22973,1102,51767,1,66,1101,0,1,67,1102,930,1,68,1102,1,556,69,1101,1,0,71,1102,932,1,72,1105,1,73,1,1283,8,84338,1102,1,103387,66,1101,3,0,67,1102,961,1,68,1102,302,1,69,1101,1,0,71,1102,967,1,72,1105,1,73,0,0,0,0,0,0,29,95714,1102,1,17959,66,1102,1,1,67,1102,1,996,68,1101,0,556,69,1101,0,0,71,1101,0,998,72,1106,0,73,1,1171,1101,30971,0,66,1101,1,0,67,1102,1025,1,68,1102,1,556,69,1102,1,1,71,1102,1,1027,72,1106,0,73,1,2719,5,310161,1102,42169,1,66,1101,0,4,67,1101,0,1056,68,1102,1,302,69,1102,1,1,71,1101,0,1064,72,1105,1,73,0,0,0,0,0,0,0,0,31,98506,1101,86689,0,66,1102,1,2,67,1101,0,1093,68,1101,0,302,69,1101,1,0,71,1101,1097,0,72,1105,1,73,0,0,0,0,41,459805,1101,0,10259,66,1102,3,1,67,1102,1126,1,68,1101,0,302,69,1102,1,1,71,1101,1132,0,72,1106,0,73,0,0,0,0,0,0,29,143571,1101,0,81967,66,1102,1,1,67,1102,1,1161,68,1102,1,556,69,1102,4,1,71,1101,1163,0,72,1105,1,73,1,5,35,146518,10,64378,10,128756,11,59051,1101,96851,0,66,1101,0,1,67,1101,1198,0,68,1102,556,1,69,1102,1,1,71,1102,1,1200,72,1106,0,73,1,25471,45,11197,1102,1,35771,66,1101,2,0,67,1101,1229,0,68,1101,302,0,69,1101,1,0,71,1102,1,1233,72,1105,1,73,0,0,0,0,29,47857,1101,0,67853,66,1102,1,1,67,1102,1262,1,68,1102,1,556,69,1101,0,0,71,1102,1264,1,72,1106,0,73,1,1214,1102,1,83561,66,1101,0,1,67,1101,0,1291,68,1101,556,0,69,1102,1,1,71,1101,1293,0,72,1106,0,73,1,8087,35,219777,1101,92657,0,66,1102,1,3,67,1102,1,1322,68,1102,302,1,69,1101,0,1,71,1101,1328,0,72,1105,1,73,0,0,0,0,0,0,36,26951,1102,32251,1,66,1101,1,0,67,1101,1357,0,68,1101,0,556,69,1102,1,8,71,1101,0,1359,72,1105,1,73,1,1,44,69557,9,10259,45,22394,3,35771,5,206774,35,73259,8,42169,14,278788,1102,94771,1,66,1102,1,1,67,1101,1402,0,68,1101,556,0,69,1102,1,1,71,1102,1,1404,72,1105,1,73,1,15,14,69697,1101,0,59051,66,1102,6,1,67,1101,0,1433,68,1101,302,0,69,1101,1,0,71,1102,1,1445,72,1106,0,73,0,0,0,0,0,0,0,0,0,0,0,0,43,132982,1101,0,49253,66,1101,2,0,67,1101,0,1474,68,1102,1,302,69,1101,0,1,71,1101,1478,0,72,1106,0,73,0,0,0,0,36,107804,1102,1,32189,66,1102,4,1,67,1102,1507,1,68,1102,302,1,69,1102,1,1,71,1102,1,1515,72,1106,0,73,0,0,0,0,0,0,0,0,11,118102,1101,0,26951,66,1102,1,4,67,1101,1544,0,68,1102,1,253,69,1101,1,0,71,1101,1552,0,72,1105,1,73,0,0,0,0,0,0,0,0,43,66491,1102,101891,1,66,1102,1,1,67,1101,1581,0,68,1101,0,556,69,1101,2,0,71,1102,1583,1,72,1105,1,73,1,19,41,367844,14,139394,1102,9473,1,66,1102,1,1,67,1102,1,1614,68,1102,556,1,69,1102,1,1,71,1102,1,1616,72,1105,1,73,1,125,10,96567,1102,1,57557,66,1101,1,0,67,1101,0,1645,68,1101,556,0,69,1102,1,6,71,1101,0,1647,72,1106,0,73,1,29182,31,49253,27,45946,27,68919,6,92657,6,185314,6,277971,1102,1,17053,66,1102,1,1,67,1101,0,1686,68,1102,1,556,69,1102,1,1,71,1101,0,1688,72,1105,1,73,1,-203,14,209091,1101,0,61751,66,1102,1,1,67,1102,1,1717,68,1101,0,556,69,1102,0,1,71,1101,0,1719,72,1105,1,73,1,1520,1101,0,11197,66,1102,1,2,67,1101,1746,0,68,1102,302,1,69,1101,0,1,71,1101,1750,0,72,1105,1,73,0,0,0,0,3,71542,1102,1,33679,66,1101,1,0,67,1102,1779,1,68,1101,556,0,69,1102,1,1,71,1101,1781,0,72,1106,0,73,1,7,41,551766,1102,64891,1,66,1101,0,1,67,1101,0,1810,68,1101,556,0,69,1102,1,1,71,1101,0,1812,72,1106,0,73,1,35,8,126507,1101,0,3889,66,1102,1,1,67,1102,1841,1,68,1101,0,556,69,1101,0,1,71,1101,1843,0,72,1105,1,73,1,29,41,183922,1101,0,93871,66,1101,0,1,67,1102,1872,1,68,1101,0,556,69,1102,0,1,71,1101,1874,0,72,1105,1,73,1,1785,1101,104479,0,66,1102,1,1,67,1102,1,1901,68,1102,1,556,69,1101,0,1,71,1102,1903,1,72,1105,1,73,1,-5,41,91961,1101,0,83579,66,1101,0,1,67,1101,0,1932,68,1102,1,556,69,1101,0,0,71,1101,0,1934,72,1105,1,73,1,1058,1102,44179,1,66,1101,0,1,67,1101,0,1961,68,1101,0,556,69,1101,0,2,71,1101,0,1963,72,1106,0,73,1,2,11,177153,11,354306,1102,1,56167,66,1101,0,1,67,1102,1994,1,68,1101,556,0,69,1102,1,0,71,1101,1996,0,72,1106,0,73,1,1391,1101,0,22973,66,1101,3,0,67,1102,2023,1,68,1102,1,302,69,1102,1,1,71,1102,2029,1,72,1105,1,73,0,0,0,0,0,0,36,80853,1101,47857,0,66,1102,3,1,67,1101,0,2058,68,1101,253,0,69,1102,1,1,71,1101,2064,0,72,1106,0,73,0,0,0,0,0,0,26,86689,1102,1,43951,66,1102,1,1,67,1101,2093,0,68,1102,1,556,69,1101,0,1,71,1102,1,2095,72,1105,1,73,1,11213,44,139114,1101,39359,0,66,1101,1,0,67,1101,2124,0,68,1101,556,0,69,1102,1,2,71,1101,0,2126,72,1105,1,73,1,10,10,32189,11,295255,1101,102953,0,66,1102,1,1,67,1101,2157,0,68,1102,1,556,69,1102,0,1,71,1102,1,2159,72,1106,0,73,1,1816,1101,0,5569,66,1102,1,1,67,1101,0,2186,68,1101,0,556,69,1102,1,0,71,1102,2188,1,72,1105,1,73,1,1059,1102,1,23063,66,1102,1,1,67,1101,2215,0,68,1101,556,0,69,1101,0,1,71,1102,2217,1,72,1106,0,73,1,49,5,103387

M solutions/day23.c => solutions/day23.c +176 -3
@@ 1,15 1,188 @@
#include "aoc.h"
#include "intcode/intcode.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>

struct queue {
	int head, tail, cap;
	long its[];
};

static const size_t QSIZ = sizeof(struct queue);

static struct queue *
qpush(struct queue *q, long v)
{
	if (q->tail - q->head == q->cap) {
		q = realloc(q, QSIZ + q->cap * 2 * sizeof(long));
		q->cap *= 2;
	} else if (q->tail == q->cap) {
		memmove(q->its, q->its + q->head, (q->tail - q->head) * sizeof(long));
		q->tail = q->tail - q->head;
		q->head = 0;
	}

	q->its[q->tail++] = v;
	return q;
}

static long
qpop(struct queue *q)
{
	q->head++;
	return q->its[q->head - 1];
}

static bool
qempty(const struct queue *q)
{
	return q->tail - q->head == 0;
}

static struct queue *
qinit(void)
{
	struct queue *q = malloc(QSIZ + 1024 * sizeof(long));
	q->cap = 1024;
	q->head = 0;
	q->tail = 0;
	return q;
}

static struct intcode BASE_IC;

struct input
parse_day23(FILE *in)
{
	return (struct input){ 0 };
	long *inp = parse_intcode(in), len = inp[0], *rprg = inp + 1;
	long *prg = calloc(sizeof(long), len * 5);
	memcpy(prg, rprg, sizeof(long) * len);
	BASE_IC = intc_new(prg, len * 5);
	free(inp);
	return (struct input){ prg, true };
}

static const int NC = 50;

void
part_one_day23(void *input)
{}
{
	struct intcode comps[50];
	struct queue *qs[50];
	for (int i = 0; i < NC; ++i) {
		comps[i] = intc_dup(BASE_IC);
		intc_set_input(comps + i, i);
		qs[i] = qinit();
	}

	for (;;) {
		for (int i = 0; i < NC; ++i) {
			struct intcode_st st = intc_run(comps + i);
			switch (st.st) {
			case IC_OUTPUT: {
				long addr = st.out;
				long x = intc_run(comps + i).out;
				long y = intc_run(comps + i).out;
				if (addr == 255) {
					printf("%ld", y);
					goto one_end;
				} else {
					qs[addr] = qpush(qs[addr], x);
					qs[addr] = qpush(qs[addr], y);
				}
				break;
			}
			case IC_NEED_INPUT:
				if (!qempty(qs[i])) {
					long x = qpop(qs[i]);
					long y = qpop(qs[i]);
					intc_set_input(comps + i, x);
					intc_run(comps + i);
					intc_set_input(comps + i, y);
				} else {
					intc_set_input(comps + i, -1);
				}
				break;
			default:
				assert(false);
			}
		}
	}

one_end:
	for (int i = 0; i < NC; ++i) {
		intc_free(comps[i]);
		free(qs[i]);
	}
}

void
part_two_day23(void *input)
{}
{
	struct intcode comps[50];
	struct queue *qs[50];
	bool have_nat = false;
	struct { long x, y; } nat = { 0 };
	long lasty = -1;

	for (int i = 0; i < NC; ++i) {
		comps[i] = intc_dup(BASE_IC);
		intc_set_input(comps + i, i);
		qs[i] = qinit();
	}

	for (;;) {
		bool idle = true;
		for (int i = 0; i < NC; ++i) {
			struct intcode_st st = intc_run(comps + i);
			switch (st.st) {
			case IC_OUTPUT: {
				idle = false;
				long addr = st.out;
				long x = intc_run(comps + i).out;
				long y = intc_run(comps + i).out;
				if (addr == 255) {
					nat.x = x;
					nat.y = y;
					have_nat = true;
				} else {
					qs[addr] = qpush(qs[addr], x);
					qs[addr] = qpush(qs[addr], y);
				}
				break;
			}
			case IC_NEED_INPUT:
				if (!qempty(qs[i])) {
					idle = false;
					long x = qpop(qs[i]);
					long y = qpop(qs[i]);
					intc_set_input(comps + i, x);
					intc_run(comps + i);
					intc_set_input(comps + i, y);
				} else {
					intc_set_input(comps + i, -1);
				}
				break;
			default:
				assert(false);
			}
		}

		if (idle && have_nat) {
			if (nat.y == lasty)
				break;
			lasty = nat.y;
			qs[0] = qpush(qs[0], nat.x);
			qs[0] = qpush(qs[0], nat.y);
		}
	}

	printf("%ld", lasty);

	for (int i = 0; i < NC; ++i) {
		intc_free(comps[i]);
		free(qs[i]);
	}
}