From ffc49a59a17ae685b632cf98a341b20fff545239 Mon Sep 17 00:00:00 2001 From: Adam Spragg Date: Thu, 29 Dec 2022 23:59:17 +0000 Subject: Puzzle 19: Move the timing loop into the build function --- 19.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/19.c b/19.c index 265d163..e4d52f7 100644 --- a/19.c +++ b/19.c @@ -149,19 +149,30 @@ inventory_build(struct inventory * i, struct blueprint const * b, enum MATERIAL void inventory_build_basic(struct inventory * i, struct blueprint const * b, int remaining) { - (void) remaining; + int t; - while (inventory_build(i, b, GEOD) == 0) - ; + for (t = 0; t < remaining; ++t) { + while (inventory_build(i, b, GEOD) == 0) + ; - while (inventory_build(i, b, BSDN) == 0) - ; + while (inventory_build(i, b, BSDN) == 0) + ; - while (inventory_build(i, b, CLAY) == 0) - ; + while (inventory_build(i, b, CLAY) == 0) + ; - while (inventory_build(i, b, ORE) == 0) - ; + while (inventory_build(i, b, ORE) == 0) + ; + + inventory_tick(i); + + fprintf(stderr, "Blueprint %d tick %d, ore: %ld/%ld, clay %ld/%ld, bsdn %ld/%ld, geod %ld/%ld\n", + b->id, t + 1, + i->ore.amount, i->ore.robots, + i->clay.amount, i->clay.robots, + i->bsdn.amount, i->bsdn.robots, + i->bsdn.amount, i->bsdn.robots); + } } @@ -184,7 +195,6 @@ main() while (fgets(buf, sizeof(buf), stdin)) { regmatch_t rematch[8]; - int t; struct blueprint b; struct inventory i; @@ -203,18 +213,8 @@ main() atoi(buf + rematch[7].rm_so)); inventory_init(&i); - for (t = 0; t < 24; ++t) { - inventory_build_basic(&i, &b, 24 - t); - inventory_tick(&i); - - fprintf(stderr, "Blueprint %d tick %d, ore: %ld/%ld, clay %ld/%ld, bsdn %ld/%ld, geod %ld/%ld\n", - b.id, t + 1, - i.ore.amount, i.ore.robots, - i.clay.amount, i.clay.robots, - i.bsdn.amount, i.bsdn.robots, - i.bsdn.amount, i.bsdn.robots); - } + inventory_build_basic(&i, &b, 24); printf("Blueprint %d geodes = %ld\n", b.id, i.geod.amount); } -- cgit v1.2.1