From 535d37ca4279effed0eedbdac9f4a6c686e2c0af Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Sat, 14 Dec 2019 14:43:20 +0100 Subject: [PATCH] Implement day 14 part 2. --- 2019/src/day14.cpp | 28 ++++++++++++++++++++++++---- 2019/tests/samples/14-2-1.in | 1 + 2019/tests/samples/14-2-1.out | 1 + 2019/tests/samples/14-2-2.in | 1 + 2019/tests/samples/14-2-2.out | 1 + 2019/tests/samples/14-2-3.in | 1 + 2019/tests/samples/14-2-3.out | 1 + 7 files changed, 30 insertions(+), 4 deletions(-) create mode 120000 2019/tests/samples/14-2-1.in create mode 100644 2019/tests/samples/14-2-1.out create mode 120000 2019/tests/samples/14-2-2.in create mode 100644 2019/tests/samples/14-2-2.out create mode 120000 2019/tests/samples/14-2-3.in create mode 100644 2019/tests/samples/14-2-3.out diff --git a/2019/src/day14.cpp b/2019/src/day14.cpp index 773f4c4..f20c07c 100644 --- a/2019/src/day14.cpp +++ b/2019/src/day14.cpp @@ -99,16 +99,36 @@ namespace { return ore_needed; } + + std::int64_t ore_to_fuel(const std::map &recipes, std::int64_t amount = 1) { + auto inverted = element_creators(recipes); + std::unordered_map stock; + + return ore_required("FUEL", amount, stock, recipes, inverted); + } } void aoc2019::day14_part1(std::istream &input, std::ostream &output) { auto recipes = read_recipes(input); - auto inverted = element_creators(recipes); - std::unordered_map stock; - output << ore_required("FUEL", 1, stock, recipes, inverted) << std::endl; + output << ore_to_fuel(recipes) << std::endl; } void aoc2019::day14_part2(std::istream &input, std::ostream &output) { - output << "Not implemented\n"; + auto recipes = read_recipes(input); + + constexpr std::int64_t ore_stock = 1000000000000; + + std::int64_t min = 1, max = ore_stock + 1; // assumption: 1 ore produces < 1 fuel. + while (max - min > 1) { + auto cur = (max + min) / 2; + + if (ore_to_fuel(recipes, cur) < ore_stock) { + min = cur; + } else { + max = cur - 1; + } + } + + output << (max + min) / 2 << std::endl; } diff --git a/2019/tests/samples/14-2-1.in b/2019/tests/samples/14-2-1.in new file mode 120000 index 0000000..e5a7725 --- /dev/null +++ b/2019/tests/samples/14-2-1.in @@ -0,0 +1 @@ +14-1-3.in \ No newline at end of file diff --git a/2019/tests/samples/14-2-1.out b/2019/tests/samples/14-2-1.out new file mode 100644 index 0000000..421714d --- /dev/null +++ b/2019/tests/samples/14-2-1.out @@ -0,0 +1 @@ +82892753 diff --git a/2019/tests/samples/14-2-2.in b/2019/tests/samples/14-2-2.in new file mode 120000 index 0000000..78b977b --- /dev/null +++ b/2019/tests/samples/14-2-2.in @@ -0,0 +1 @@ +14-1-4.in \ No newline at end of file diff --git a/2019/tests/samples/14-2-2.out b/2019/tests/samples/14-2-2.out new file mode 100644 index 0000000..c55239b --- /dev/null +++ b/2019/tests/samples/14-2-2.out @@ -0,0 +1 @@ +5586022 diff --git a/2019/tests/samples/14-2-3.in b/2019/tests/samples/14-2-3.in new file mode 120000 index 0000000..7e9ed9d --- /dev/null +++ b/2019/tests/samples/14-2-3.in @@ -0,0 +1 @@ +14-1-5.in \ No newline at end of file diff --git a/2019/tests/samples/14-2-3.out b/2019/tests/samples/14-2-3.out new file mode 100644 index 0000000..1a5d12f --- /dev/null +++ b/2019/tests/samples/14-2-3.out @@ -0,0 +1 @@ +460664