summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Spragg <adam@spra.gg>2022-12-29 23:59:17 +0000
committerAdam Spragg <adam@spra.gg>2022-12-29 23:59:17 +0000
commitffc49a59a17ae685b632cf98a341b20fff545239 (patch)
tree324afd622cfd8791dd68b1f35ee0e19b09682661
parentdd9fe7e11dd42bd01898078732cb2fae30778a05 (diff)
Puzzle 19: Move the timing loop into the build function
-rw-r--r--19.c42
1 files 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);
}