aboutsummaryrefslogtreecommitdiffstats
path: root/src/21/main.c
blob: 10e561bf61a8c2369c3d0570bd55b42ae1f6c137 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "iccmp.h"
#include "maxint.h"
#include "aoc.h"
#include "util.h"

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

void
consume_output(struct icc *icc)
{
	struct maxint mi;
	mi_ul imm;

	mi.cap = 0;
	mi.data = &imm;
	mi.size = 1;
	mi.sign = MI_POS;
	imm = 128;

	while (icc->state != ICC_HALT) {
		switch (icc->state) {
		case ICC_OUTPUT:
			if (mi_cmp(&icc->out, &mi) >= 0) {
				aoc_debug("LARGE\n");
				return;
			}
			aoc_debug("%c", (char) mi_cast_ul(&icc->out));
			break;
		case ICC_INPUT:
			return;
		}
		icc_step_inst(icc);
	}
}

void
feed_input(struct icc *icc, const char *line)
{
	const char *c;

	consume_output(icc);

	c = line;
	while (icc->state != ICC_HALT) {
		switch (icc->state) {
		case ICC_INPUT:
			mi_setv(&icc->in, (mi_ul) *c, MI_POS);
			c += 1;
			break;
		}
		icc_step_inst(icc);
		if (!*c) return;
	}
}

void
part1(void)
{
	struct icc icc;

	icc_init(&icc);
	icc_parse_inst(&icc, aoc.input, aoc.input_size);

	feed_input(&icc, "NOT C J\n");
	feed_input(&icc, "AND D J\n");
	feed_input(&icc, "NOT A T\n");
	feed_input(&icc, "OR T J\n");
	feed_input(&icc, "WALK\n");

	consume_output(&icc);
	assert(icc.state == ICC_OUTPUT);

	aoc.answer = aprintf("%lu", mi_cast_ul(&icc.out));
	aoc.solution = "19358262";

	icc_deinit(&icc);
}

void
part2(void)
{
	struct icc icc;

	icc_init(&icc);
	icc_parse_inst(&icc, aoc.input, aoc.input_size);

	feed_input(&icc, "NOT C J\n");
	feed_input(&icc, "NOT B T\n");
	feed_input(&icc, "OR T J\n");
	feed_input(&icc, "AND D J\n");
	feed_input(&icc, "AND H J\n");
	feed_input(&icc, "NOT A T\n");
	feed_input(&icc, "OR T J\n");
	feed_input(&icc, "RUN\n");

	consume_output(&icc);
	assert(icc.state == ICC_OUTPUT);

	aoc.answer = aprintf("%lu", mi_cast_ul(&icc.out));
	aoc.solution = "1142686742";

	icc_deinit(&icc);
}