aboutsummaryrefslogtreecommitdiffstats
path: root/src/13/main.c
blob: e2a6f29a3cb9bd83835e7c67c57b9db82ec8a601 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "aoc.h"
#include "allocator.h"
#include "iccmp.h"
#include "hmap.h"
#include "maxint.h"
#include "util.h"

#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

const char *tiles = " +#=o";

struct vec2 {
	int x, y;
};

enum {
	EMPTY,
	WALL,
	BLOCK,
	PADDLE,
	BALL
};

uint32_t
hmap_pos_hash(struct hmap_key key)
{
	const struct vec2 *pos = key.p;
	uint32_t hash;

	hash = ((uint32_t) pos->x & 0xffffffff);
	hash = (hash << 8) ^ ((uint32_t) pos->y & 0xffffffff);

	return hash;
}

bool
hmap_pos_keycmp(struct hmap_key k1, struct hmap_key k2)
{
	const struct vec2 *v1 = k1.p, *v2 = k2.p;

	return v1->x == v2->x && v1->y == v2->y;
}

void
print_map(struct hmap *map)
{
	struct hmap_iter iter;
	const struct vec2 *ppos;
	struct vec2 size, pos;
	struct hmap_link *link;
	char tile;
	int x, y;

	size.x = size.y = 1;
	for (HMAP_ITER(map, iter)) {
		ppos = iter.link->key.p;
		size.x = MAX(size.x, ppos->x + 1);
		size.y = MAX(size.y, ppos->y + 1);
	}

	for (y = 0; y < size.y; y++) {
		for (x = 0; x < size.x; x++) {
			pos.x = x;
			pos.y = y;
			link = hmap_get(map, (struct hmap_key) {.p = &pos});
			if (link)
				tile = tiles[link->value.i];
			else
				tile = tiles[EMPTY];
			aoc_debug("%c", tile);
		}
		aoc_debug("\n");
	}

	aoc_debug("\n");
}

void
part1(void)
{
	struct hmap_link **link;
	struct hmap_iter iter;
	struct hmap map;
	struct icc icc;
	struct vec2 pos;
	int outind, outvec[3];
	int rc, count;
	void *key;

	hmap_init(&map, 100, hmap_pos_hash, hmap_pos_keycmp,
		&stdlib_strict_heap_allocator);

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

	outind = 0;
	while (icc.state != ICC_HALT) {
		icc_step_inst(&icc);
		switch (icc.state) {
		case ICC_OUTPUT:
			outvec[outind] = (int) mi_cast_ul(&icc.out);
			outind = (outind + 1) % 3;
			if (!outind) {
				pos.x = outvec[0];
				pos.y = outvec[1];
				link = hmap_link_pos(&map,
					(struct hmap_key) {.p = &pos});
				if (*link) {
					(*link)->value.i = outvec[2];
				} else {
					key = memdup(&pos, sizeof(pos));
					*link = hmap_link_alloc(&map,
						(struct hmap_key) {.p = key},
						(struct hmap_val) {.i = outvec[2]},
						&rc);
					assert(*link);
				}
			}
			break;
		}
	}

	count = 0;
	for (HMAP_ITER(&map, iter)) {
		if (iter.link->value.i == BLOCK)
			count += 1;
	}

	aoc.answer = aprintf("%i", count);
	aoc.solution = "329";

	for (HMAP_ITER(&map, iter))
		free(iter.link->key._p);
	hmap_deinit(&map);
	icc_deinit(&icc);
}

void
part2(void)
{
	struct hmap_link **link;
	struct hmap_iter iter;
	struct hmap map;
	struct maxint addr = MI_INIT;
	struct maxint val = MI_INIT;
	mi_ul addr_ul, val_ul;
	struct icc icc;
	struct vec2 pos, ball, paddle;
	int outind, outvec[3];
	int rc, score, dir;
	void *key;

	hmap_init(&map, 100, hmap_pos_hash, hmap_pos_keycmp,
		&stdlib_strict_heap_allocator);

	icc_init(&icc);
	icc_parse_inst(&icc, aoc.input, aoc.input_size);
	icc_write(&icc, mi_imm(&addr, &addr_ul, 0, MI_POS),
		mi_imm(&val, &val_ul, 2, MI_POS));

	score = 0;
	outind = 0;
	while (icc.state != ICC_HALT) {
		icc_step_inst(&icc);
		switch (icc.state) {
		case ICC_INPUT:
			if (paddle.x < ball.x) {
				mi_setv(&icc.in, 1, MI_POS);
				dir = 1;
			} else if (paddle.x > ball.x) {
				mi_setv(&icc.in, 1, MI_NEG);
				dir = -1;
			} else {
				mi_setv(&icc.in, 0, MI_POS);
				dir = 0;
			}
			if (aoc.debug) {
				aoc_debug("Score: %i, Ball: %i %i, Paddle: %i %i, Dir: %i\n",
					score, ball.x, ball.y, paddle.x, paddle.y, dir);
				print_map(&map);
			}
			break;
		case ICC_OUTPUT:
			outvec[outind] = (int) mi_cast_ul(&icc.out)
				* (icc.out.sign == MI_POS ? 1 : -1);
			outind = (outind + 1) % 3;
			if (!outind) {
				pos.x = outvec[0];
				pos.y = outvec[1];
				if (pos.x == -1 && pos.y == 0) {
					score = outvec[2];
				} else {
					link = hmap_link_pos(&map,
						(struct hmap_key) {.p = &pos});
					if (*link) {
						(*link)->value.i = outvec[2];
					} else {
						key = memdup(&pos, sizeof(pos));
						*link = hmap_link_alloc(&map,
							(struct hmap_key) {.p = key},
							(struct hmap_val) {.i = outvec[2]},
							&rc);
						assert(*link);
					}
					if (outvec[2] == BALL) {
						ball.x = pos.x;
						ball.y = pos.y;
					} else if (outvec[2] == PADDLE) {
						paddle.x = pos.x;
						paddle.y = pos.y;
					}
				}
			}
			break;
		}
	}

	aoc.answer = aprintf("%i", score);
	aoc.solution = "15973";

	for (HMAP_ITER(&map, iter))
		free(iter.link->key._p);
	hmap_deinit(&map);
	icc_deinit(&icc);
}