From 0cc5d88296361fa347a0b6b79d662a6cd8882fa4 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Thu, 13 Dec 2018 17:40:39 -0500 Subject: Solve day 13 part 2 --- 2018/day13.c | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) (limited to '2018') 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); + } } } -- cgit 1.4.1 ounce/commit/bounce.c?id=d4b828f8d66b83749a1a43d298053c67dd7d29b6&follow=1'>Report paths in unveil errorsJune McEnroe 2020-10-24contrib/palaver: Use open_memstream instead of fmemopenJune McEnroe 2020-10-11Handle signals before the main loopJune McEnroe 2020-09-09Fix possibliy uninitialized error 2.0June McEnroe 2020-08-31Refactor reserialization and client self-producingJune McEnroe 2020-08-30Add chmod+chown to certbot exampleJune McEnroe 2020-08-30Rearrange bounce.c, move non-main mains below mainJune McEnroe 2020-08-30Sandbox pounce with unveil(2)June McEnroe 2020-08-28Refactor certificate loading and load all certs from config pathsJune McEnroe 2020-08-27Sandbox pounce with pledge(2)June McEnroe 2020-08-27Sandbox calico with pledge(2) and unveil(2)June McEnroe 2020-08-27Add support for OpenBSDJune McEnroe 2020-08-27Remove rc scriptsJune McEnroe 2020-08-27contrib/palaver: Fix documented database pathJune McEnroe 2020-08-27contrib/palaver: Remove rc scriptJune McEnroe 2020-08-27contrib/palaver: Fix database search and creationJune McEnroe 2020-08-27contrib/palaver: Use pounce's XDG directoryJune McEnroe 2020-08-27contrib/palaver: Only allow HTTPSJune McEnroe 2020-08-25Support the pounce_env rc variableJune McEnroe 2020-08-25Remove deprecated option namesJune McEnroe 2020-08-25Document configuration and data file searchJune McEnroe 2020-08-24Use dataOpen for save fileJune McEnroe 2020-08-24Use configOpen to load localCAJune McEnroe 2020-08-24Use configPath to load client cert/privJune McEnroe 2020-08-24Use configOpen in getopt_configJune McEnroe 2020-08-24Import xdg.c from catgirlJune McEnroe 2020-08-23Replace “RAND_bytes” by “getentropy”Issam E. Maghni 2020-08-16contrib/palaver: Add no message preview flagsJune McEnroe 2020-08-13contrib/palaver: Don't set channel for PMsJune McEnroe 2020-08-13Fix unintended interception of NICK after registrationJune McEnroe 2020-08-12Add Additional Components section to READMEJune McEnroe 2020-08-12Document -L / palaver optionJune McEnroe 2020-08-11contrib/palaver: Document service configurationJune McEnroe 2020-08-11contrib/palaver: Add install target and rc scriptJune McEnroe 2020-08-11contrib/palaver: Implement command and notificationsJune McEnroe