Better use of move and copy constructors.

This commit is contained in:
2019-12-02 19:40:18 +01:00
parent 3fc7390eaa
commit 403b2889a2

View File

@@ -14,7 +14,7 @@ static std::vector<int> read_program(std::istream &input) {
return program; return program;
} }
static int run_program(std::vector<int> &program) { static int run_program(std::vector<int> program) {
for (int ip = 0; ip < program.size(); ip += 4) { for (int ip = 0; ip < program.size(); ip += 4) {
switch (program[ip]) { switch (program[ip]) {
case 1: case 1:
@@ -42,18 +42,17 @@ void aoc2019::day02_part1(std::istream &input, std::ostream &output) {
auto program = read_program(input); auto program = read_program(input);
program[1] = 12; program[1] = 12;
program[2] = 2; program[2] = 2;
output << run_program(program) << std::endl; output << run_program(std::move(program)) << std::endl;
} }
void aoc2019::day02_part2(std::istream &input, std::ostream &output) { void aoc2019::day02_part2(std::istream &input, std::ostream &output) {
const auto program = read_program(input); auto program = read_program(input);
for (int noun = 0; noun < 100; ++noun) { for (int noun = 0; noun < 100; ++noun) {
for (int verb = 0; verb < 100; ++verb) { for (int verb = 0; verb < 100; ++verb) {
auto program_copy = program; program[1] = noun;
program_copy[1] = noun; program[2] = verb;
program_copy[2] = verb; if (run_program(program) == 19690720) {
if (run_program(program_copy) == 19690720) {
output << 100 * noun + verb << std::endl; output << 100 * noun + verb << std::endl;
return; return;
} }