diff options
Diffstat (limited to '19.c')
-rw-r--r-- | 19.c | 93 |
1 files changed, 93 insertions, 0 deletions
@@ -89,6 +89,27 @@ inventory_init(struct inventory * i) } +int +inventory_cmp(struct inventory const * a, struct inventory const * b) +{ + if (a->geod.amount != b->geod.amount) + return a->geod.amount - b->geod.amount; + if (a->geod.robots != b->geod.robots) + return a->geod.robots - b->geod.robots; + if (a->bsdn.amount != b->bsdn.amount) + return a->bsdn.amount - b->bsdn.amount; + if (a->bsdn.robots != b->bsdn.robots) + return a->bsdn.robots - b->bsdn.robots; + if (a->clay.amount != b->clay.amount) + return a->clay.amount - b->clay.amount; + if (a->clay.robots != b->clay.robots) + return a->clay.robots - b->clay.robots; + if (a->ore.amount != b->ore.amount) + return a->ore.amount - b->ore.amount; + return a->ore.robots - b->ore.robots; +} + + void inventory_tick(struct inventory * i) { @@ -178,6 +199,68 @@ inventory_build_basic(struct inventory * i, struct blueprint const * b, int rema } +long exhaustive_iter; + + +void +inventory_build_exhaustive(struct inventory * i, struct blueprint const * b, int remaining) +{ + struct inventory best, test; + + if (!remaining) + // No time to do anything! + return; + + ++exhaustive_iter; + + // Try doing nothing else this round. + best = *i; + inventory_tick(&best); +// fprintf(stderr, "%d: doing nothing\n", remaining); + inventory_build_exhaustive(&best, b, remaining - 1); + + test = *i; + + // Try building a geode robot, and check if that's better than doing nothing. + if (inventory_build(&test, b, GEOD) == 0) { +// fprintf(stderr, "%d: building a geod robot (%ld)\n", remaining, test.geod.amount); + inventory_build_exhaustive(&test, b, remaining); + if (inventory_cmp(&test, &best) > 0) + best = test; + test = *i; + } + + // Try building a obsidian robot, and checking if that's better than anything else + if (inventory_build(&test, b, BSDN) == 0) { +// fprintf(stderr, "%d: building a bsdn robot (%ld)\n", remaining, test.bsdn.amount); + inventory_build_exhaustive(&test, b, remaining); + if (inventory_cmp(&test, &best) > 0) + best = test; + test = *i; + } + + // Try building a clay robot, and checking if that's better than anything else + if (inventory_build(&test, b, CLAY) == 0) { +// fprintf(stderr, "%d: building a clay robot (%ld)\n", remaining, test.clay.amount); + inventory_build_exhaustive(&test, b, remaining); + if (inventory_cmp(&test, &best) > 0) + best = test; + test = *i; + } + + // Try building a ore robot, and checking if that's better than anything else + if (inventory_build(&test, b, ORE) == 0) { +// fprintf(stderr, "%d: building a ore robot (%ld)\n", remaining, test.ore.amount); + inventory_build_exhaustive(&test, b, remaining); + if (inventory_cmp(&test, &best) > 0) + best = test; + test = *i; + } + + *i = best; +} + + int main(int argc, char ** argv) { @@ -238,6 +321,16 @@ main(int argc, char ** argv) if (strcmp(build, "basic") == 0) { inventory_build_basic(&i, &b, rounds); } + else if (strcmp(build, "exhaustive") == 0) { + exhaustive_iter = 0; + inventory_build_exhaustive(&i, &b, rounds); + fprintf(stdout, "Blueprint %d, iterations %ld, ore %ld/%ld, clay %ld/%ld, bsdn %ld/%ld, geod %ld/%ld\n", + b.id, exhaustive_iter, + i.ore.amount, i.ore.robots, + i.clay.amount, i.clay.robots, + i.bsdn.amount, i.bsdn.robots, + i.geod.amount, i.geod.robots); + } else { fprintf(stderr, "Unknown build strategy: %s\n", build); regfree(&reblueprint); |