-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7.h
89 lines (83 loc) · 2.39 KB
/
day7.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <algorithm>
#include <fstream>
#include <optional>
#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;
}
pair<optional<int>, bool> run(int& pos, vector<int>& program, optional<int> input) {
optional<int> output;
for (int op; (op = program[pos] % 100) != 3 || input;) {
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;
input.reset();
} 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;
} else if (op == 99) {
return { output, true };
}
}
return { output, false };
};
int loop(const vector<int>& program, const vector<int>& phases) {
bool halted = false;
vector<pair<int, vector<int>>> states;
for (int phase : phases) {
auto& state = states.emplace_back(0, program);
tie(ignore, halted) = run(state.first, state.second, phase);
}
optional<int> output = 0;
while (!halted) {
for (auto& [pos, program] : states) {
tie(output, halted) = run(pos, program, output);
}
}
return *output;
}
int part1(ifstream input) {
auto program = parse(move(input));
vector<int> phases { 0, 1, 2, 3, 4 };
int maximum = 0;
do {
maximum = max(maximum, loop(program, phases));
} while (next_permutation(begin(phases), end(phases)));
return maximum;
}
int part2(ifstream input) {
auto program = parse(move(input));
vector<int> phases { 5, 6, 7, 8, 9 };
int maximum = 0;
do {
maximum = max(maximum, loop(program, phases));
} while (next_permutation(begin(phases), end(phases)));
return maximum;
}
}