diff --git a/res/2015/day23/test_input.txt b/res/2015/day23/test_input.txt index e69de29..b8ba985 100644 --- a/res/2015/day23/test_input.txt +++ b/res/2015/day23/test_input.txt @@ -0,0 +1,4 @@ +inc b +jio b, +2 +tpl b +inc b diff --git a/src/2015/Day23.cpp b/src/2015/Day23.cpp index 5cbe0d5..1fd9b49 100644 --- a/src/2015/Day23.cpp +++ b/src/2015/Day23.cpp @@ -2,30 +2,121 @@ namespace y2015::day23 { - REGISTER_DAY(2015, Day23, std::vector, int); + enum class Operation + { + Half, + Triple, + Increment, + Jump, + JumpIfEven, + JumpIfOne, + }; - REGISTER_TEST_EXAMPLE(2015, Day23, ExampleInput, 1, 0); - REGISTER_TEST(2015, Day23, Input, 1, 0); - REGISTER_TEST_EXAMPLE(2015, Day23, ExampleInput, 2, 0); - REGISTER_TEST(2015, Day23, Input, 2, 0); + struct Instruction + { + Operation operation; + char reg; + int argument; + }; + + REGISTER_DAY(2015, Day23, std::vector, 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) { - std::vector vec; + std::vector vec; std::string 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; } + int64_t Run(const std::vector& instructions, std::map 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) { - return 0; + return Run(input, std::map{}); } OUTPUT2(input) { - return 0; + std::map registers; + registers['a'] = 1; + return Run(input, registers); } }