diff options
author | kaiwu <kaiwu2004@gmail.com> | 2023-01-17 14:46:43 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2023-01-17 14:46:43 +0800 |
commit | 19cacb76086943e60f030be3b99f7361c9958666 (patch) | |
tree | fa75e1d6c947b2d9c04a8dbf673e86a9ae164918 /src | |
parent | 66d054fde394792f2826d94908d0fd0c7f32aed4 (diff) | |
download | advent-of-code-19cacb76086943e60f030be3b99f7361c9958666.tar.gz advent-of-code-19cacb76086943e60f030be3b99f7361c9958666.zip |
2022 day19 part1 done
Diffstat (limited to 'src')
-rw-r--r-- | src/2022/day19/README.md | 206 | ||||
-rw-r--r-- | src/2022/day19/aoc.cpp | 74 |
2 files changed, 261 insertions, 19 deletions
diff --git a/src/2022/day19/README.md b/src/2022/day19/README.md index 85b3e9f..6a533b5 100644 --- a/src/2022/day19/README.md +++ b/src/2022/day19/README.md @@ -165,3 +165,209 @@ Determine the quality level of each blueprint by multiplying that blueprint's ID Determine the quality level of each blueprint using the largest number of geodes it could produce in 24 minutes. What do you get if you add up the quality level of all of the blueprints in your list? +--- Part Two --- +While you were choosing the best blueprint, the elephants found some food on their own, so you're not in as much of a hurry; you figure you probably have 32 minutes before the wind changes direction again and you'll need to get out of range of the erupting volcano. + +Unfortunately, one of the elephants ate most of your blueprint list! Now, only the first three blueprints in your list are intact. + +In 32 minutes, the largest number of geodes blueprint 1 (from the example above) can open is 56. One way to achieve that is: + +== Minute 1 == +1 ore-collecting robot collects 1 ore; you now have 1 ore. + +== Minute 2 == +1 ore-collecting robot collects 1 ore; you now have 2 ore. + +== Minute 3 == +1 ore-collecting robot collects 1 ore; you now have 3 ore. + +== Minute 4 == +1 ore-collecting robot collects 1 ore; you now have 4 ore. + +== Minute 5 == +Spend 4 ore to start building an ore-collecting robot. +1 ore-collecting robot collects 1 ore; you now have 1 ore. +The new ore-collecting robot is ready; you now have 2 of them. + +== Minute 6 == +2 ore-collecting robots collect 2 ore; you now have 3 ore. + +== Minute 7 == +Spend 2 ore to start building a clay-collecting robot. +2 ore-collecting robots collect 2 ore; you now have 3 ore. +The new clay-collecting robot is ready; you now have 1 of them. + +== Minute 8 == +Spend 2 ore to start building a clay-collecting robot. +2 ore-collecting robots collect 2 ore; you now have 3 ore. +1 clay-collecting robot collects 1 clay; you now have 1 clay. +The new clay-collecting robot is ready; you now have 2 of them. + +== Minute 9 == +Spend 2 ore to start building a clay-collecting robot. +2 ore-collecting robots collect 2 ore; you now have 3 ore. +2 clay-collecting robots collect 2 clay; you now have 3 clay. +The new clay-collecting robot is ready; you now have 3 of them. + +== Minute 10 == +Spend 2 ore to start building a clay-collecting robot. +2 ore-collecting robots collect 2 ore; you now have 3 ore. +3 clay-collecting robots collect 3 clay; you now have 6 clay. +The new clay-collecting robot is ready; you now have 4 of them. + +== Minute 11 == +Spend 2 ore to start building a clay-collecting robot. +2 ore-collecting robots collect 2 ore; you now have 3 ore. +4 clay-collecting robots collect 4 clay; you now have 10 clay. +The new clay-collecting robot is ready; you now have 5 of them. + +== Minute 12 == +Spend 2 ore to start building a clay-collecting robot. +2 ore-collecting robots collect 2 ore; you now have 3 ore. +5 clay-collecting robots collect 5 clay; you now have 15 clay. +The new clay-collecting robot is ready; you now have 6 of them. + +== Minute 13 == +Spend 2 ore to start building a clay-collecting robot. +2 ore-collecting robots collect 2 ore; you now have 3 ore. +6 clay-collecting robots collect 6 clay; you now have 21 clay. +The new clay-collecting robot is ready; you now have 7 of them. + +== Minute 14 == +Spend 3 ore and 14 clay to start building an obsidian-collecting robot. +2 ore-collecting robots collect 2 ore; you now have 2 ore. +7 clay-collecting robots collect 7 clay; you now have 14 clay. +The new obsidian-collecting robot is ready; you now have 1 of them. + +== Minute 15 == +2 ore-collecting robots collect 2 ore; you now have 4 ore. +7 clay-collecting robots collect 7 clay; you now have 21 clay. +1 obsidian-collecting robot collects 1 obsidian; you now have 1 obsidian. + +== Minute 16 == +Spend 3 ore and 14 clay to start building an obsidian-collecting robot. +2 ore-collecting robots collect 2 ore; you now have 3 ore. +7 clay-collecting robots collect 7 clay; you now have 14 clay. +1 obsidian-collecting robot collects 1 obsidian; you now have 2 obsidian. +The new obsidian-collecting robot is ready; you now have 2 of them. + +== Minute 17 == +Spend 3 ore and 14 clay to start building an obsidian-collecting robot. +2 ore-collecting robots collect 2 ore; you now have 2 ore. +7 clay-collecting robots collect 7 clay; you now have 7 clay. +2 obsidian-collecting robots collect 2 obsidian; you now have 4 obsidian. +The new obsidian-collecting robot is ready; you now have 3 of them. + +== Minute 18 == +2 ore-collecting robots collect 2 ore; you now have 4 ore. +7 clay-collecting robots collect 7 clay; you now have 14 clay. +3 obsidian-collecting robots collect 3 obsidian; you now have 7 obsidian. + +== Minute 19 == +Spend 3 ore and 14 clay to start building an obsidian-collecting robot. +2 ore-collecting robots collect 2 ore; you now have 3 ore. +7 clay-collecting robots collect 7 clay; you now have 7 clay. +3 obsidian-collecting robots collect 3 obsidian; you now have 10 obsidian. +The new obsidian-collecting robot is ready; you now have 4 of them. + +== Minute 20 == +Spend 2 ore and 7 obsidian to start building a geode-cracking robot. +2 ore-collecting robots collect 2 ore; you now have 3 ore. +7 clay-collecting robots collect 7 clay; you now have 14 clay. +4 obsidian-collecting robots collect 4 obsidian; you now have 7 obsidian. +The new geode-cracking robot is ready; you now have 1 of them. + +== Minute 21 == +Spend 3 ore and 14 clay to start building an obsidian-collecting robot. +2 ore-collecting robots collect 2 ore; you now have 2 ore. +7 clay-collecting robots collect 7 clay; you now have 7 clay. +4 obsidian-collecting robots collect 4 obsidian; you now have 11 obsidian. +1 geode-cracking robot cracks 1 geode; you now have 1 open geode. +The new obsidian-collecting robot is ready; you now have 5 of them. + +== Minute 22 == +Spend 2 ore and 7 obsidian to start building a geode-cracking robot. +2 ore-collecting robots collect 2 ore; you now have 2 ore. +7 clay-collecting robots collect 7 clay; you now have 14 clay. +5 obsidian-collecting robots collect 5 obsidian; you now have 9 obsidian. +1 geode-cracking robot cracks 1 geode; you now have 2 open geodes. +The new geode-cracking robot is ready; you now have 2 of them. + +== Minute 23 == +Spend 2 ore and 7 obsidian to start building a geode-cracking robot. +2 ore-collecting robots collect 2 ore; you now have 2 ore. +7 clay-collecting robots collect 7 clay; you now have 21 clay. +5 obsidian-collecting robots collect 5 obsidian; you now have 7 obsidian. +2 geode-cracking robots crack 2 geodes; you now have 4 open geodes. +The new geode-cracking robot is ready; you now have 3 of them. + +== Minute 24 == +Spend 2 ore and 7 obsidian to start building a geode-cracking robot. +2 ore-collecting robots collect 2 ore; you now have 2 ore. +7 clay-collecting robots collect 7 clay; you now have 28 clay. +5 obsidian-collecting robots collect 5 obsidian; you now have 5 obsidian. +3 geode-cracking robots crack 3 geodes; you now have 7 open geodes. +The new geode-cracking robot is ready; you now have 4 of them. + +== Minute 25 == +2 ore-collecting robots collect 2 ore; you now have 4 ore. +7 clay-collecting robots collect 7 clay; you now have 35 clay. +5 obsidian-collecting robots collect 5 obsidian; you now have 10 obsidian. +4 geode-cracking robots crack 4 geodes; you now have 11 open geodes. + +== Minute 26 == +Spend 2 ore and 7 obsidian to start building a geode-cracking robot. +2 ore-collecting robots collect 2 ore; you now have 4 ore. +7 clay-collecting robots collect 7 clay; you now have 42 clay. +5 obsidian-collecting robots collect 5 obsidian; you now have 8 obsidian. +4 geode-cracking robots crack 4 geodes; you now have 15 open geodes. +The new geode-cracking robot is ready; you now have 5 of them. + +== Minute 27 == +Spend 2 ore and 7 obsidian to start building a geode-cracking robot. +2 ore-collecting robots collect 2 ore; you now have 4 ore. +7 clay-collecting robots collect 7 clay; you now have 49 clay. +5 obsidian-collecting robots collect 5 obsidian; you now have 6 obsidian. +5 geode-cracking robots crack 5 geodes; you now have 20 open geodes. +The new geode-cracking robot is ready; you now have 6 of them. + +== Minute 28 == +2 ore-collecting robots collect 2 ore; you now have 6 ore. +7 clay-collecting robots collect 7 clay; you now have 56 clay. +5 obsidian-collecting robots collect 5 obsidian; you now have 11 obsidian. +6 geode-cracking robots crack 6 geodes; you now have 26 open geodes. + +== Minute 29 == +Spend 2 ore and 7 obsidian to start building a geode-cracking robot. +2 ore-collecting robots collect 2 ore; you now have 6 ore. +7 clay-collecting robots collect 7 clay; you now have 63 clay. +5 obsidian-collecting robots collect 5 obsidian; you now have 9 obsidian. +6 geode-cracking robots crack 6 geodes; you now have 32 open geodes. +The new geode-cracking robot is ready; you now have 7 of them. + +== Minute 30 == +Spend 2 ore and 7 obsidian to start building a geode-cracking robot. +2 ore-collecting robots collect 2 ore; you now have 6 ore. +7 clay-collecting robots collect 7 clay; you now have 70 clay. +5 obsidian-collecting robots collect 5 obsidian; you now have 7 obsidian. +7 geode-cracking robots crack 7 geodes; you now have 39 open geodes. +The new geode-cracking robot is ready; you now have 8 of them. + +== Minute 31 == +Spend 2 ore and 7 obsidian to start building a geode-cracking robot. +2 ore-collecting robots collect 2 ore; you now have 6 ore. +7 clay-collecting robots collect 7 clay; you now have 77 clay. +5 obsidian-collecting robots collect 5 obsidian; you now have 5 obsidian. +8 geode-cracking robots crack 8 geodes; you now have 47 open geodes. +The new geode-cracking robot is ready; you now have 9 of them. + +== Minute 32 == +2 ore-collecting robots collect 2 ore; you now have 8 ore. +7 clay-collecting robots collect 7 clay; you now have 84 clay. +5 obsidian-collecting robots collect 5 obsidian; you now have 10 obsidian. +9 geode-cracking robots crack 9 geodes; you now have 56 open geodes. +However, blueprint 2 from the example above is still better; using it, the largest number of geodes you could open in 32 minutes is 62. + +You no longer have enough blueprints to worry about quality levels. Instead, for each of the first three blueprints, determine the largest number of geodes you could open; then, multiply these three values together. + +Don't worry about quality levels; instead, just determine the largest number of geodes you could open using each of the first three blueprints. What do you get if you multiply these numbers together? diff --git a/src/2022/day19/aoc.cpp b/src/2022/day19/aoc.cpp index 923505b..d1b2455 100644 --- a/src/2022/day19/aoc.cpp +++ b/src/2022/day19/aoc.cpp @@ -53,24 +53,53 @@ bool can_build(const blueprint& b, const build_result& r, int i) { } // i = 3,2,1,0 -void build_product(int x, const blueprint& b, build_result r, std::vector<build_result>& rs) { - if (x < 0) { - build_product(r, 5); - rs.push_back(r); - } else { +// Each robot can collect 1 of its resource type per minute. +// It also takes one minute for the robot factory (also conveniently from your pack) to construct any type of robot, +// although it consumes the necessary resources available when construction begins. +// 1 24| ms: 20 23 11 5 | rs: 4 10 6 2 +// 2 24| ms: 0 0 0 0 | rs: 1 0 0 0 +// 3 24| ms: 4 11 6 2 | rs: 2 3 2 1 +// 4 24| ms: 9 37 8 10 | rs: 3 9 6 4 +// 5 24| ms: 13 28 7 3 | rs: 4 9 4 2 +// 6 24| ms: 6 16 6 6 | rs: 3 5 6 3 +// 7 24| ms: 0 0 0 0 | rs: 1 0 0 0 +// 8 24| ms: 4 2 8 11 | rs: 2 2 5 3 +// 9 24| ms: 6 35 8 3 | rs: 4 11 5 2 +// 10 24| ms: 12 17 15 8 | rs: 4 5 8 3 +// 11 24| ms: 1 7 7 1 | rs: 1 3 4 1 +// 12 24| ms: 0 0 0 0 | rs: 1 0 0 0 +// 13 24| ms: 3 7 6 1 | rs: 2 3 4 1 +// 14 24| ms: 0 0 0 0 | rs: 1 0 0 0 +// 15 24| ms: 3 39 9 15 | rs: 3 7 7 5 +// 16 24| ms: 0 0 0 0 | rs: 1 0 0 0 +// 17 24| ms: 0 0 0 0 | rs: 1 0 0 0 +// 18 24| ms: 4 23 12 12 | rs: 2 5 7 4 +// 19 24| ms: 0 0 0 0 | rs: 1 0 0 0 +// 20 24| ms: 10 11 13 2 | rs: 4 11 6 1 +// 21 24| ms: 4 24 7 1 | rs: 2 9 4 1 +// 22 24| ms: 0 0 0 0 | rs: 1 0 0 0 +// 23 24| ms: 2 8 14 3 | rs: 1 4 4 1 +// 24 24| ms: 3 38 14 14 | rs: 2 8 8 4 +// 25 24| ms: 4 15 10 1 | rs: 3 8 5 1 +// 26 24| ms: 21 17 10 1 | rs: 4 11 6 1 +// 27 24| ms: 8 13 7 2 | rs: 4 9 4 1 +// 28 24| ms: 0 0 0 0 | rs: 1 0 0 0 +// 29 24| ms: 1 21 8 9 | rs: 1 3 2 2 +// 30 24| ms: 2 10 4 1 | rs: 2 6 3 1 +void build_product(const blueprint& b, build_result r, std::vector<build_result>& rs) { + bool done{false}; + for (int i = 3; i >= 0 && !done; i--) { auto r0 = r; - bool built{false}; - for (int i = x; i >= 0; i--) { - while (can_build(b, r0, i)) { - built = true; - r0 = build_robot(i, r0, b); - build_product(r0, i); - } - } - if (built) { + if (can_build(b, r0, i)) { + r0 = build_robot(i, r0, b); + build_product(r0, i); rs.push_back(r0); + done = i > 1; // best senario } - build_product(x - 1, b, r, rs); + } + if (!done) { + build_product(r, 4); + rs.push_back(r); } } @@ -78,7 +107,7 @@ void build(int m, const blueprint& b, build_result r, build_result& max) { // print_result(b.idx, 24 - m, r); if (m > 0) { std::vector<build_result> rs; - build_product(3, b, r, rs); + build_product(b, r, rs); for (auto& r0 : rs) { build(m - 1, b, r0, max); } @@ -101,11 +130,18 @@ std::pair<int, int> day19(line_view file) { build_result r; build_result m; // b.print(); - build(24, b, r, m); - print_result(b.idx, 24, m); + build(32, b, r, m); + print_result(b.idx, 32, m); rs.push_back(m); } - return {0, 0}; + int quality{0}; + for (size_t i = 0; i < rs.size(); i++) { + quality += (i+1) * rs[i].products[3]; + } + + printf("%d\n", quality); + + return {1624, 0}; } } // namespace aoc2022 |