summary refs log tree commit diff homepage
path: root/2018/day13.c
blob: d6be93d7155e702e854f2ea366a409eb4391cc90 (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
#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;
	char done;
};

int main() {
	struct Cell map[200][200];
	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[y][x++] = (struct Cell) { '|', ch, '[', 0 };
		} else if (ch == '<' || ch == '>') {
			map[y][x++] = (struct Cell) { '-', ch, '[', 0 };
		} else {
			map[y][x++].track = ch;
		}
	}

	for (;;) {
		for (y = 0; y < 200; ++y) {
			for (x = 0; x < 200; ++x) {
				if (!map[y][x].cart || map[y][x].done) continue;
				uint ny = y, nx = x;
				switch (map[y][x].cart) {
					break; case '^': ny--;
					break; case 'v': ny++;
					break; case '<': nx--;
					break; case '>': nx++;
				}
				if (map[ny][nx].cart) {
					printf("%u,%u\n", nx, ny);
					map[y][x].cart = 0;
					map[ny][nx].cart = 0;
					continue;
				}
				map[ny][nx].cart = map[y][x].cart;
				map[ny][nx].turn = map[y][x].turn;
				map[ny][nx].done = 1;
				map[y][x].cart = 0;
				struct Cell *cell = &map[ny][nx];
				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';
					}
				}
			}
		}
		uint carts = 0;
		for (y = 0; y < 200; ++y) {
			for (x = 0; x < 200; ++x) {
				if (map[y][x].cart) carts++;
				map[y][x].done = 0;
			}
		}
		if (carts == 1) break;
	}

	for (y = 0; y < 200; ++y) {
		for (x = 0; x < 200; ++x) {
			if (map[y][x].cart) printf("%u,%u\n", x, y);
		}
	}
}