Add solution for 2016 Day 12
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
cpy 41 a
|
||||||
|
inc a
|
||||||
|
inc a
|
||||||
|
dec a
|
||||||
|
jnz a 2
|
||||||
|
dec a
|
||||||
|
|||||||
+93
-8
@@ -2,30 +2,115 @@
|
|||||||
|
|
||||||
namespace y2016::day12
|
namespace y2016::day12
|
||||||
{
|
{
|
||||||
REGISTER_DAY(2016, Day12, std::vector<int>, int);
|
enum class Operator
|
||||||
|
{
|
||||||
|
Cpy,
|
||||||
|
Inc,
|
||||||
|
Dec,
|
||||||
|
Jnz
|
||||||
|
};
|
||||||
|
|
||||||
REGISTER_TEST_EXAMPLE(2016, Day12, ExampleInput, 1, 0);
|
struct Instruction
|
||||||
REGISTER_TEST(2016, Day12, Input, 1, 0);
|
{
|
||||||
REGISTER_TEST_EXAMPLE(2016, Day12, ExampleInput, 2, 0);
|
Operator op;
|
||||||
REGISTER_TEST(2016, Day12, Input, 2, 0);
|
std::variant<char, int> arg1;
|
||||||
|
std::variant<char, int> arg2;
|
||||||
|
};
|
||||||
|
|
||||||
|
REGISTER_DAY(2016, Day12, std::vector<Instruction>, int);
|
||||||
|
|
||||||
|
REGISTER_TEST_EXAMPLE(2016, Day12, ExampleInput, 1, 42);
|
||||||
|
REGISTER_TEST(2016, Day12, Input, 1, 318007);
|
||||||
|
REGISTER_TEST_EXAMPLE(2016, Day12, ExampleInput, 2, 42);
|
||||||
|
REGISTER_TEST(2016, Day12, Input, 2, 9227661);
|
||||||
|
|
||||||
READ_INPUT(input)
|
READ_INPUT(input)
|
||||||
{
|
{
|
||||||
std::vector<int> vec;
|
std::vector<Instruction> vec;
|
||||||
std::string str;
|
std::string str;
|
||||||
while (getline(input, str))
|
while (getline(input, str))
|
||||||
{
|
{
|
||||||
|
Instruction instruction;
|
||||||
|
if (str[0] == 'c')
|
||||||
|
{
|
||||||
|
instruction.op = Operator::Cpy;
|
||||||
|
if (str[4] >= 'a' && str[4] <= 'd')
|
||||||
|
instruction.arg1 = str[4];
|
||||||
|
else
|
||||||
|
instruction.arg1 = (int)std::atoi(str.c_str() + 4);
|
||||||
|
instruction.arg2 = str[str.size() - 1];
|
||||||
|
}
|
||||||
|
else if (str[0] == 'i')
|
||||||
|
{
|
||||||
|
instruction.op = Operator::Inc;
|
||||||
|
instruction.arg1 = str[4];
|
||||||
|
}
|
||||||
|
else if (str[0] == 'd')
|
||||||
|
{
|
||||||
|
instruction.op = Operator::Dec;
|
||||||
|
instruction.arg1 = str[4];
|
||||||
|
}
|
||||||
|
else if (str[0] == 'j')
|
||||||
|
{
|
||||||
|
instruction.op = Operator::Jnz;
|
||||||
|
if (str[4] >= 'a' && str[4] <= 'd')
|
||||||
|
instruction.arg1 = str[4];
|
||||||
|
else
|
||||||
|
instruction.arg1 = (int)std::atoi(str.c_str() + 4);
|
||||||
|
instruction.arg2 = atoi(str.c_str() + 6);
|
||||||
|
}
|
||||||
|
vec.emplace_back(instruction);
|
||||||
}
|
}
|
||||||
return vec;
|
return vec;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Run(const std::vector<Instruction>& instructions, std::vector<int> registers)
|
||||||
|
{
|
||||||
|
size_t pc = 0;
|
||||||
|
while (pc < instructions.size())
|
||||||
|
{
|
||||||
|
switch (instructions[pc].op)
|
||||||
|
{
|
||||||
|
case Operator::Cpy:
|
||||||
|
{
|
||||||
|
int number = 0;
|
||||||
|
if (std::holds_alternative<int>(instructions[pc].arg1))
|
||||||
|
number = std::get<int>(instructions[pc].arg1);
|
||||||
|
else
|
||||||
|
number = registers[std::get<char>(instructions[pc].arg1) - 'a'];
|
||||||
|
registers[std::get<char>(instructions[pc].arg2) - 'a'] = number;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Operator::Inc:
|
||||||
|
registers[std::get<char>(instructions[pc].arg1) - 'a']++;
|
||||||
|
break;
|
||||||
|
case Operator::Dec:
|
||||||
|
registers[std::get<char>(instructions[pc].arg1) - 'a']--;
|
||||||
|
break;
|
||||||
|
case Operator::Jnz:
|
||||||
|
int value = 0;
|
||||||
|
if (std::holds_alternative<int>(instructions[pc].arg1))
|
||||||
|
value = std::get<int>(instructions[pc].arg1);
|
||||||
|
else
|
||||||
|
value = registers[std::get<char>(instructions[pc].arg1) - 'a'];
|
||||||
|
if (value != 0)
|
||||||
|
pc += std::get<int>(instructions[pc].arg2) - 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
pc++;
|
||||||
|
}
|
||||||
|
return registers[0];
|
||||||
|
}
|
||||||
|
|
||||||
OUTPUT1(input)
|
OUTPUT1(input)
|
||||||
{
|
{
|
||||||
return 0;
|
return Run(input, std::vector<int>(4));
|
||||||
}
|
}
|
||||||
|
|
||||||
OUTPUT2(input)
|
OUTPUT2(input)
|
||||||
{
|
{
|
||||||
return 0;
|
std::vector<int> registers(4);
|
||||||
|
registers[2] = 1;
|
||||||
|
return Run(input, registers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user