diff options
author | June McEnroe <june@causal.agency> | 2018-12-13 17:40:39 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2020-11-22 00:14:25 -0500 |
commit | 68b1406f089624140a4865192a3bfdd4921e3661 (patch) | |
tree | a4b5562d3e0ae183d0641185c9fc7ca5cd6625cf | |
parent | Solve day 13 part 1 (diff) | |
download | aoc-68b1406f089624140a4865192a3bfdd4921e3661.tar.gz aoc-68b1406f089624140a4865192a3bfdd4921e3661.zip |
Solve day 13 part 2
-rw-r--r-- | 2018/day13.c | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/2018/day13.c b/2018/day13.c index f367392..d6be93d 100644 --- a/2018/day13.c +++ b/2018/day13.c @@ -10,14 +10,11 @@ struct Cell { char track; char cart; char turn; -}; - -struct Map { - struct Cell cells[200][200]; + char done; }; int main() { - struct Map map; + struct Cell map[200][200]; memset(&map, 0, sizeof(map)); uint y = 0, x = 0; char ch; @@ -26,34 +23,36 @@ int main() { y++; x = 0; } else if (ch == '^' || ch == 'v') { - map.cells[y][x++] = (struct Cell) { '|', ch, '[' }; + map[y][x++] = (struct Cell) { '|', ch, '[', 0 }; } else if (ch == '<' || ch == '>') { - map.cells[y][x++] = (struct Cell) { '-', ch, '[' }; + map[y][x++] = (struct Cell) { '-', ch, '[', 0 }; } else { - map.cells[y][x++].track = ch; + map[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; + if (!map[y][x].cart || map[y][x].done) continue; uint ny = y, nx = x; - switch (map.cells[y][x].cart) { + switch (map[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) { + if (map[ny][nx].cart) { printf("%u,%u\n", nx, ny); - exit(EXIT_SUCCESS); + map[y][x].cart = 0; + map[ny][nx].cart = 0; + continue; } - cell->cart = map.cells[y][x].cart; - cell->turn = map.cells[y][x].turn; + 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 = '<'; @@ -86,6 +85,19 @@ int main() { } } } - map = next; + 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); + } } } |