mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implement day 2.
This commit is contained in:
1
2019/inputs/02.txt
Normal file
1
2019/inputs/02.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,6,19,23,1,10,23,27,2,27,13,31,1,31,6,35,2,6,35,39,1,39,5,43,1,6,43,47,2,6,47,51,1,51,5,55,2,55,9,59,1,6,59,63,1,9,63,67,1,67,10,71,2,9,71,75,1,6,75,79,1,5,79,83,2,83,10,87,1,87,5,91,1,91,9,95,1,6,95,99,2,99,10,103,1,103,5,107,2,107,6,111,1,111,5,115,1,9,115,119,2,119,10,123,1,6,123,127,2,13,127,131,1,131,6,135,1,135,10,139,1,13,139,143,1,143,13,147,1,5,147,151,1,151,2,155,1,155,5,0,99,2,0,14,0
|
||||||
@@ -1,10 +1,63 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
#include <exception>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <array>
|
||||||
|
#include <vector>
|
||||||
#include "days.hpp"
|
#include "days.hpp"
|
||||||
|
|
||||||
|
static std::vector<int> read_program(std::istream &input) {
|
||||||
|
std::vector<int> program{};
|
||||||
|
for (int current; input >> current; input.ignore()) {
|
||||||
|
program.push_back(current);
|
||||||
|
}
|
||||||
|
|
||||||
|
return program;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int run_program(std::vector<int> &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");
|
||||||
|
}
|
||||||
|
|
||||||
void aoc2019::day02_part1(std::istream &input, std::ostream &output) {
|
void aoc2019::day02_part1(std::istream &input, std::ostream &output) {
|
||||||
output << "Not implemented\n";
|
auto program = read_program(input);
|
||||||
|
program[1] = 12;
|
||||||
|
program[2] = 2;
|
||||||
|
output << run_program(program) << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void aoc2019::day02_part2(std::istream &input, std::ostream &output) {
|
void aoc2019::day02_part2(std::istream &input, std::ostream &output) {
|
||||||
output << "Not implemented\n";
|
const auto program = read_program(input);
|
||||||
|
|
||||||
|
for (int noun = 0; noun < 100; ++noun) {
|
||||||
|
for (int verb = 0; verb < 100; ++verb) {
|
||||||
|
auto program_copy = program;
|
||||||
|
program_copy[1] = noun;
|
||||||
|
program_copy[2] = verb;
|
||||||
|
if (run_program(program_copy) == 19690720) {
|
||||||
|
output << 100 * noun + verb << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw std::domain_error("No valid solution.");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user