Added older advents
This commit is contained in:
parent
8db2505049
commit
9cf858b860
78 changed files with 13807 additions and 0 deletions
79
advent_of_code_2019/day2/main2.cc
Normal file
79
advent_of_code_2019/day2/main2.cc
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
vector<int> read_input()
|
||||
{
|
||||
vector<int> rval;
|
||||
int buffer;
|
||||
char comma;
|
||||
|
||||
while (cin)
|
||||
{
|
||||
cin >> buffer;
|
||||
rval.push_back(buffer);
|
||||
cin >> comma;
|
||||
}
|
||||
|
||||
return rval;
|
||||
}
|
||||
|
||||
int run_program(vector<int> program, int noun = 12, int verb = 2)
|
||||
{
|
||||
cout << "running program!\n";
|
||||
size_t instruction = 0;
|
||||
|
||||
program[1] = noun;
|
||||
program[2] = verb;
|
||||
|
||||
while(true)
|
||||
{
|
||||
switch(program[instruction++])
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
size_t lhs = program[instruction++];
|
||||
size_t rhs = program[instruction++];
|
||||
size_t result = program[instruction++];
|
||||
program[result] = program[lhs] + program[rhs];
|
||||
}
|
||||
break;
|
||||
|
||||
case 2:
|
||||
{
|
||||
size_t lhs = program[instruction++];
|
||||
size_t rhs = program[instruction++];
|
||||
size_t result = program[instruction++];
|
||||
program[result] = program[lhs] * program[rhs];
|
||||
}
|
||||
break;
|
||||
|
||||
case 99:
|
||||
return program[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
vector<int> program = read_input();
|
||||
int target_output = 19690720;
|
||||
|
||||
for (int noun = 0; noun < 100; ++noun)
|
||||
{
|
||||
cout << "\rIteration: " << noun << " / 100";
|
||||
for (int verb = 0; verb < 100; ++verb)
|
||||
{
|
||||
int result = run_program(program, noun, verb);
|
||||
|
||||
if (result == target_output)
|
||||
{
|
||||
cout << "Noun: " << noun << "\n";
|
||||
cout << "Verb: " << verb << "\n";
|
||||
cout << "Answer: " << (100 * noun + verb) << "\n";
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue