summary refs log tree commit diff homepage
path: root/2018
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-12-13 16:39:59 -0500
committerJune McEnroe <june@causal.agency>2020-11-22 00:14:25 -0500
commit35e8159ac215fc5f96aa4b6791f239feab2f8568 (patch)
treeee54f68fe7aa200d9159f968f1ac0430e7324504 /2018
parentSolve day 12 part 2 (diff)
downloadaoc-35e8159ac215fc5f96aa4b6791f239feab2f8568.tar.gz
aoc-35e8159ac215fc5f96aa4b6791f239feab2f8568.zip
Solve day 13 part 1
Diffstat (limited to '2018')
-rw-r--r--2018/day13.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/2018/day13.c b/2018/day13.c
new file mode 100644
index 0000000..f367392
--- /dev/null
+++ b/2018/day13.c
@@ -0,0 +1,91 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef unsigned uint;
+
+#define T(x, y) ((uint)(x) << 8 | (uint)(y))
+
+struct Cell {
+	char track;
+	char cart;
+	char turn;
+};
+
+struct Map {
+	struct Cell cells[200][200];
+};
+
+int main() {
+	struct Map map;
+	memset(&map, 0, sizeof(map));
+	uint y = 0, x = 0;
+	char ch;
+	while (EOF != (ch = getchar())) {
+		if (ch == '\n') {
+			y++;
+			x = 0;
+		} else if (ch == '^' || ch == 'v') {
+			map.cells[y][x++] = (struct Cell) { '|', ch, '[' };
+		} else if (ch == '<' || ch == '>') {
+			map.cells[y][x++] = (struct Cell) { '-', ch, '[' };
+		} else {
+			map.cells[y][x++].track = ch;
+		}
+	}
+
+	for (;;) {
+		struct Map next = map;
+		for (y = 0; y < 200; ++y) {
+			for (x = 0; x < 200; ++x) {
+				if (!map.cells[y][x].cart) continue;
+				next.cells[y][x].cart = 0;
+				uint ny = y, nx = x;
+				switch (map.cells[y][x].cart) {
+					break; case '^': ny--;
+					break; case 'v': ny++;
+					break; case '<': nx--;
+					break; case '>': nx++;
+				}
+				struct Cell *cell = &next.cells[ny][nx];
+				if (cell->cart) {
+					printf("%u,%u\n", nx, ny);
+					exit(EXIT_SUCCESS);
+				}
+				cell->cart = map.cells[y][x].cart;
+				cell->turn = map.cells[y][x].turn;
+				switch (T(cell->cart, cell->track)) {
+					break; case T('^', '/'):  cell->cart = '>';
+					break; case T('^', '\\'): cell->cart = '<';
+					break; case T('v', '/'):  cell->cart = '<';
+					break; case T('v', '\\'): cell->cart = '>';
+					break; case T('<', '/'):  cell->cart = 'v';
+					break; case T('<', '\\'): cell->cart = '^';
+					break; case T('>', '/'):  cell->cart = '^';
+					break; case T('>', '\\'): cell->cart = 'v';
+					break; case T('^', '+'): switch (cell->turn) {
+						break; case '[': cell->turn = '|'; cell->cart = '<';
+						break; case '|': cell->turn = ']';
+						break; case ']': cell->turn = '['; cell->cart = '>';
+					}
+					break; case T('v', '+'): switch (cell->turn) {
+						break; case '[': cell->turn = '|'; cell->cart = '>';
+						break; case '|': cell->turn = ']';
+						break; case ']': cell->turn = '['; cell->cart = '<';
+					}
+					break; case T('<', '+'): switch (cell->turn) {
+						break; case '[': cell->turn = '|'; cell->cart = 'v';
+						break; case '|': cell->turn = ']';
+						break; case ']': cell->turn = '['; cell->cart = '^';
+					}
+					break; case T('>', '+'): switch (cell->turn) {
+						break; case '[': cell->turn = '|'; cell->cart = '^';
+						break; case '|': cell->turn = ']';
+						break; case ']': cell->turn = '['; cell->cart = 'v';
+					}
+				}
+			}
+		}
+		map = next;
+	}
+}