-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5.h
58 lines (53 loc) · 1.36 KB
/
day5.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <fstream>
#include <vector>
namespace {
using namespace std;
vector<int> parse(ifstream input) {
vector<int> program;
int value;
while (input >> value) {
program.push_back(value);
input.ignore(1, ',');
}
return program;
}
int run(vector<int> program, int input) {
int output;
int pos = 0;
while (program[pos] != 99) {
int op = program[pos] % 100;
auto param = [&, mode = program[pos++] / 10]() mutable -> int& {
mode /= 10;
if (mode % 10 == 0)
return program[program[pos++]];
if (mode % 10 == 1)
return program[pos++];
};
if (op == 1) {
param() = param() + param();
} else if (op == 2) {
param() = param() * param();
} else if (op == 3) {
param() = input;
} else if (op == 4) {
output = param();
} else if (op == 5) {
pos = param() != 0 ? param() : pos + 1;
} else if (op == 6) {
pos = param() == 0 ? param() : pos + 1;
} else if (op == 7) {
int left = param();
param() = left < param() ? 1 : 0;
} else if (op == 8) {
param() = param() == param() ? 1 : 0;
}
}
return output;
}
int part1(ifstream input) {
return run(parse(move(input)), 1);
}
int part2(ifstream input) {
return run(parse(move(input)), 5);
}
}