Add solution for 2015 Day 23
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
inc b
|
||||||
|
jio b, +2
|
||||||
|
tpl b
|
||||||
|
inc b
|
||||||
|
|||||||
+99
-8
@@ -2,30 +2,121 @@
|
|||||||
|
|
||||||
namespace y2015::day23
|
namespace y2015::day23
|
||||||
{
|
{
|
||||||
REGISTER_DAY(2015, Day23, std::vector<int>, int);
|
enum class Operation
|
||||||
|
{
|
||||||
|
Half,
|
||||||
|
Triple,
|
||||||
|
Increment,
|
||||||
|
Jump,
|
||||||
|
JumpIfEven,
|
||||||
|
JumpIfOne,
|
||||||
|
};
|
||||||
|
|
||||||
REGISTER_TEST_EXAMPLE(2015, Day23, ExampleInput, 1, 0);
|
struct Instruction
|
||||||
REGISTER_TEST(2015, Day23, Input, 1, 0);
|
{
|
||||||
REGISTER_TEST_EXAMPLE(2015, Day23, ExampleInput, 2, 0);
|
Operation operation;
|
||||||
REGISTER_TEST(2015, Day23, Input, 2, 0);
|
char reg;
|
||||||
|
int argument;
|
||||||
|
};
|
||||||
|
|
||||||
|
REGISTER_DAY(2015, Day23, std::vector<Instruction>, uint64_t);
|
||||||
|
|
||||||
|
REGISTER_TEST_EXAMPLE(2015, Day23, ExampleInput, 1, 2);
|
||||||
|
REGISTER_TEST(2015, Day23, Input, 1, 255);
|
||||||
|
REGISTER_TEST_EXAMPLE(2015, Day23, ExampleInput, 2, 2);
|
||||||
|
REGISTER_TEST(2015, Day23, Input, 2, 334);
|
||||||
|
|
||||||
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;
|
||||||
|
std::stringstream ss{str};
|
||||||
|
std::string s;
|
||||||
|
char c;
|
||||||
|
ss >> s;
|
||||||
|
// std::cout << s << std::endl;
|
||||||
|
if (s == "hlf")
|
||||||
|
{
|
||||||
|
instruction.operation = Operation::Half;
|
||||||
|
ss >> instruction.reg;
|
||||||
|
}
|
||||||
|
else if (s == "tpl")
|
||||||
|
{
|
||||||
|
instruction.operation = Operation::Triple;
|
||||||
|
ss >> instruction.reg;
|
||||||
|
}
|
||||||
|
else if (s == "inc")
|
||||||
|
{
|
||||||
|
instruction.operation = Operation::Increment;
|
||||||
|
ss >> instruction.reg;
|
||||||
|
}
|
||||||
|
else if (s == "jmp")
|
||||||
|
{
|
||||||
|
instruction.operation = Operation::Jump;
|
||||||
|
ss >> instruction.argument;
|
||||||
|
}
|
||||||
|
else if (s == "jie")
|
||||||
|
{
|
||||||
|
instruction.operation = Operation::JumpIfEven;
|
||||||
|
ss >> instruction.reg >> ", " >> instruction.argument;
|
||||||
|
}
|
||||||
|
else if (s == "jio")
|
||||||
|
{
|
||||||
|
instruction.operation = Operation::JumpIfOne;
|
||||||
|
ss >> instruction.reg >> ", " >> instruction.argument;
|
||||||
|
}
|
||||||
|
vec.emplace_back(instruction);
|
||||||
}
|
}
|
||||||
return vec;
|
return vec;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int64_t Run(const std::vector<Instruction>& instructions, std::map<char, uint64_t> registers)
|
||||||
|
{
|
||||||
|
size_t pc = 0;
|
||||||
|
while (pc < instructions.size())
|
||||||
|
{
|
||||||
|
const auto& instruction = instructions[pc];
|
||||||
|
Operation op = instruction.operation;
|
||||||
|
switch (op)
|
||||||
|
{
|
||||||
|
case Operation::Half:
|
||||||
|
registers[instruction.reg] = registers[instruction.reg] / 2;
|
||||||
|
break;
|
||||||
|
case Operation::Triple:
|
||||||
|
registers[instruction.reg] = registers[instruction.reg] * 3;
|
||||||
|
break;
|
||||||
|
case Operation::Increment:
|
||||||
|
registers[instruction.reg]++;
|
||||||
|
break;
|
||||||
|
case Operation::Jump:
|
||||||
|
pc += instruction.argument - 1;
|
||||||
|
break;
|
||||||
|
case Operation::JumpIfEven:
|
||||||
|
if (registers[instruction.reg] % 2 == 0)
|
||||||
|
pc += instruction.argument - 1;
|
||||||
|
break;
|
||||||
|
case Operation::JumpIfOne:
|
||||||
|
if (registers[instruction.reg] == 1)
|
||||||
|
pc += instruction.argument - 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
pc++;
|
||||||
|
}
|
||||||
|
return registers['b'];
|
||||||
|
}
|
||||||
|
|
||||||
OUTPUT1(input)
|
OUTPUT1(input)
|
||||||
{
|
{
|
||||||
return 0;
|
return Run(input, std::map<char, uint64_t>{});
|
||||||
}
|
}
|
||||||
|
|
||||||
OUTPUT2(input)
|
OUTPUT2(input)
|
||||||
{
|
{
|
||||||
return 0;
|
std::map<char, uint64_t> registers;
|
||||||
|
registers['a'] = 1;
|
||||||
|
return Run(input, registers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user