From fc16b73a0176ccf1489ede22a4512207d196e12e Mon Sep 17 00:00:00 2001 From: Thraix Date: Sun, 16 Aug 2026 20:29:20 +0200 Subject: [PATCH] Add solution for 2016 Day 10 --- res/2016/day10/test_input.txt | 6 ++ src/2016/Day10.cpp | 133 +++++++++++++++++++++++++++++++--- 2 files changed, 130 insertions(+), 9 deletions(-) diff --git a/res/2016/day10/test_input.txt b/res/2016/day10/test_input.txt index e69de29..e9ad2b4 100644 --- a/res/2016/day10/test_input.txt +++ b/res/2016/day10/test_input.txt @@ -0,0 +1,6 @@ +value 5 goes to bot 2 +bot 2 gives low to bot 1 and high to bot 0 +value 3 goes to bot 1 +bot 1 gives low to output 1 and high to bot 0 +bot 0 gives low to output 2 and high to output 0 +value 2 goes to bot 2 diff --git a/src/2016/Day10.cpp b/src/2016/Day10.cpp index c58da66..d38b8e0 100644 --- a/src/2016/Day10.cpp +++ b/src/2016/Day10.cpp @@ -2,30 +2,145 @@ namespace y2016::day10 { - REGISTER_DAY(2016, Day10, std::vector, int); + enum class Output + { + Output, + Bot + }; - REGISTER_TEST_EXAMPLE(2016, Day10, ExampleInput, 1, 0); - REGISTER_TEST(2016, Day10, Input, 1, 0); - REGISTER_TEST_EXAMPLE(2016, Day10, ExampleInput, 2, 0); - REGISTER_TEST(2016, Day10, Input, 2, 0); + struct Bot + { + std::vector inputs; + int lowId{-1}; + int highId{-1}; + Output lowOutput; + Output highOutput; + }; + + using Bots = std::map; + REGISTER_DAY(2016, Day10, Bots, int); + + REGISTER_TEST_EXAMPLE(2016, Day10, ExampleInput, 1, 2); + REGISTER_TEST(2016, Day10, Input, 1, 47); + REGISTER_TEST_EXAMPLE(2016, Day10, ExampleInput, 2, 30); + REGISTER_TEST(2016, Day10, Input, 2, 2666); READ_INPUT(input) { - std::vector vec; + std::map map; std::string str; while (getline(input, str)) { + std::stringstream ss{str}; + if (str[0] == 'v') + { + int value; + int id; + ss >> "value" >> value >> "goes to bot " >> id; + map[id].inputs.emplace_back(value); + } + else + { + int idFrom; + int idLow; + int idHigh; + std::string typeLow; + std::string typeHigh; + ss >> "bot " >> idFrom >> "gives low to " >> typeLow >> idLow >> "and high to " >> typeHigh >> idHigh; + Bot& from = map[idFrom]; + from.lowId = idLow; + from.highId = idHigh; + if (typeLow == "bot") + from.lowOutput = Output::Bot; + else + from.lowOutput = Output::Output; + if (typeHigh == "bot") + from.highOutput = Output::Bot; + else + from.highOutput = Output::Output; + } } - return vec; + return map; + } + + bool HasInput(const Bot& bot, int input1, int input2) + { + return bot.inputs.size() == 2 && ((bot.inputs[0] == input1 && bot.inputs[1] == input2) || + (bot.inputs[0] == input2 && bot.inputs[1] == input1)); } OUTPUT1(input) { - return 0; + int input1 = isExample ? 5 : 61; + int input2 = isExample ? 2 : 17; + std::map bots = input; + while (true) + { + for (auto it = bots.begin(); it != bots.end();) + { + if (HasInput(it->second, input1, input2)) + return it->first; + + if (it->second.inputs.size() == 2) + { + int low = Helper::Min(it->second.inputs); + int high = Helper::Max(it->second.inputs); + if (it->second.lowOutput == Output::Bot) + { + bots[it->second.lowId].inputs.emplace_back(low); + if (HasInput(bots[it->second.lowId], input1, input2)) + return it->second.lowId; + } + + if (it->second.highOutput == Output::Bot) + { + bots[it->second.highId].inputs.emplace_back(high); + if (HasInput(bots[it->second.highId], input1, input2)) + return it->second.highId; + } + it = bots.erase(it); + } + else + { + it++; + } + } + } + return -1; } OUTPUT2(input) { - return 0; + std::map outputs; + int input1 = isExample ? 5 : 61; + int input2 = isExample ? 2 : 17; + std::map bots = input; + while (!bots.empty()) + { + for (auto it = bots.begin(); it != bots.end();) + { + if (it->second.inputs.size() == 2) + { + int low = Helper::Min(it->second.inputs); + int high = Helper::Max(it->second.inputs); + if (it->second.lowOutput == Output::Bot) + bots[it->second.lowId].inputs.emplace_back(low); + else + outputs[it->second.lowId] = low; + + if (it->second.highOutput == Output::Bot) + bots[it->second.highId].inputs.emplace_back(high); + else + outputs[it->second.highId] = high; + + it = bots.erase(it); + } + else + { + it++; + } + } + } + return outputs[0] * outputs[1] * outputs[2]; } }