Add solution for 2016 Day 10
This commit is contained in:
@@ -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
|
||||
|
||||
+124
-9
@@ -2,30 +2,145 @@
|
||||
|
||||
namespace y2016::day10
|
||||
{
|
||||
REGISTER_DAY(2016, Day10, std::vector<int>, 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<int> inputs;
|
||||
int lowId{-1};
|
||||
int highId{-1};
|
||||
Output lowOutput;
|
||||
Output highOutput;
|
||||
};
|
||||
|
||||
using Bots = std::map<int, Bot>;
|
||||
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<int> vec;
|
||||
std::map<int, Bot> 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<int, Bot> 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<int, int> outputs;
|
||||
int input1 = isExample ? 5 : 61;
|
||||
int input2 = isExample ? 2 : 17;
|
||||
std::map<int, Bot> 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];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user