From 35e8159ac215fc5f96aa4b6791f239feab2f8568 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Thu, 13 Dec 2018 16:39:59 -0500 Subject: Solve day 13 part 1 --- 2018/day13.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 2018/day13.c 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 +#include +#include + +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; + } +} -- cgit 1.4.1