Something went wrong. Try again.
advent of code stuff
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158#include <algorithm>#include <iomanip>#include <iostream>#include <map>#include <numeric>#include <set>#include <sstream>#include <string>#include <unordered_set>#include <vector>
#include <cassert>
struct Instruction{ std::string opcode; int operand;};
std::ostream&operator<<(std::ostream& os, const Instruction& i);
std::vector<Instruction>flip_instruction(const std::vector<Instruction>& code, size_t index);
class ISS{public: ISS(std::vector<Instruction>& code) : code(code){}; void run_step(bool print_debug_info = false); uint32_t get_pc(void) const; int get_acc(void) const; bool sim_finish_unless_loop(void);
private: const std::vector<Instruction>& code; int acc = 0; uint32_t pc = 0;};
std::ostream&operator<<(std::ostream& os, const ISS& iss);
intmain(void){ std::vector<std::string> lines; std::string line; while (std::getline(std::cin, line)) { lines.push_back(line); }
std::vector<Instruction> code; std::transform( lines.begin(), lines.end(), std::back_inserter(code), [](const std::string& line) { return Instruction{ line.substr(0, 3), std::stoi(line.substr(4)) }; });
std::set<size_t> indices; for (size_t i = 0; i < code.size(); i++) if (code[i].opcode == "nop" || code[i].opcode == "jmp") indices.insert(i);
for (size_t index : indices) { std::cout << "simulating with flip at " << index << std::endl; std::vector<Instruction> altered_code = flip_instruction(code, index); ISS iss(altered_code);
if (iss.sim_finish_unless_loop()) { std::cout << "final acc value: " << iss.get_acc() << std::endl; return EXIT_SUCCESS; } }
return EXIT_FAILURE;}
std::vector<Instruction>flip_instruction(const std::vector<Instruction>& code, size_t index){ std::vector<Instruction> altered_code(code); altered_code[index] = { (code[index].opcode == "jmp") ? "nop" : "jmp", code[index].operand }; return altered_code;}
voidISS::run_step(bool print_debug_info){ if (print_debug_info) std::cout << (*this);
const Instruction& instr = code[this->pc];
if (print_debug_info) std::cout << " -> " << instr;
if (instr.opcode == "nop") { this->pc++; } else if (instr.opcode == "acc") { this->acc += instr.operand; this->pc++; } else if (instr.opcode == "jmp") { if (instr.operand < 0 && std::abs(instr.operand) > (int)this->pc) std::cerr << "warning: PC underflow by adding " << instr.operand << " to " << this->pc << std::endl; this->pc += instr.operand; } else { if (print_debug_info) std::cout << std::endl; std::cerr << "error: unknown instruction: " << instr.opcode << std::endl; exit(EXIT_FAILURE); }
if (print_debug_info) std::cout << " -> " << *this << std::endl;}
uint32_tISS::get_pc(void) const{ return this->pc;};
intISS::get_acc(void) const{ return this->acc;};
boolISS::sim_finish_unless_loop(void){ std::set<uint32_t> pc_values; while ((!pc_values.contains(this->pc)) && this->pc < this->code.size()) { pc_values.insert(this->pc); this->run_step(); } return this->pc >= this->code.size();}
std::ostream&operator<<(std::ostream& os, const ISS& iss){ return os << '[' << std::setw(5) << iss.get_pc() << std::setw(5) << iss.get_acc() << ']';}
std::ostream&operator<<(std::ostream& os, const Instruction& i){ return os << '(' << i.opcode << std::setw(5) << i.operand << ')';}