From 0bf29b1e423e5cfa128426193445fd042100f83b Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Thu, 5 Dec 2019 18:19:26 +0100 Subject: [PATCH] Reuse general intcode implementation. --- 2019/src/day02.cpp | 38 +++++--------------------------------- 1 file changed, 5 insertions(+), 33 deletions(-) diff --git a/2019/src/day02.cpp b/2019/src/day02.cpp index 8d9cce9..6164d4f 100644 --- a/2019/src/day02.cpp +++ b/2019/src/day02.cpp @@ -1,52 +1,24 @@ -#include #include #include #include #include #include "days.hpp" - -static std::vector read_program(std::istream &input) { - std::vector program{}; - for (int current; input >> current; input.ignore()) { - program.push_back(current); - } - - return program; -} +#include "utils.hpp" static int run_program(std::vector program) { - for (int ip = 0; ip < program.size(); ip += 4) { - switch (program[ip]) { - case 1: - program[program[ip + 3]] = program[program[ip + 1]] + program[program[ip + 2]]; - break; - - case 2: - program[program[ip + 3]] = program[program[ip + 1]] * program[program[ip + 2]]; - break; - - case 99: - return program[0]; - - default: - char buffer[30]; - std::snprintf(buffer, sizeof(buffer), "Invalid opcode: %d", program[ip]); - - throw std::domain_error(buffer); - } - } - throw std::out_of_range("Program read out of bounds"); + aoc2019::run_intcode(program); + return program[0]; } void aoc2019::day02_part1(std::istream &input, std::ostream &output) { - auto program = read_program(input); + auto program = read_intcode(input); program[1] = 12; program[2] = 2; output << run_program(std::move(program)) << std::endl; } void aoc2019::day02_part2(std::istream &input, std::ostream &output) { - auto program = read_program(input); + auto program = read_intcode(input); for (int noun = 0; noun < 100; ++noun) { for (int verb = 0; verb < 100; ++verb) {