-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle_08_2.cc
87 lines (77 loc) · 2.39 KB
/
puzzle_08_2.cc
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
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <iostream>
#include <memory>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using Registers = std::unordered_map<std::string, int>;
Registers reg;
int mm = 0;
int& getReg(const std::string& name) {
return reg.insert({name, 0}).first->second;
}
bool eval_cond(const std::vector<std::string>& words)
{
if (words[3] == "if") {
int& r = getReg(words[4]);
int val = stoi(words[6]);
std::cout << ", CR: " << words[4] << ", op: " << words[5] << ", val: " << val;
if (words[5] == "==") {
return r == val;
} else if (words[5] == ">=") {
return r >= val;
} else if (words[5] == "<=") {
return r <= val;
} else if (words[5] == "!=") {
return r != val;
} else if (words[5] == ">") {
return r > val;
} else if (words[5] == "<") {
return r < val;
} else {
std::cerr << "UNKNOWN CONDITIONAL OP: " << words[5] << "\n";
exit(1);
}
} else {
std::cerr << "UNKNOWN CONDITIONAL: " << words[3] << "\n";
exit(1);
}
}
int main()
{
std::string line;
while (std::getline(std::cin, line)) {
if (line.empty()) {
break;
}
std::vector<std::string> words;
boost::algorithm::split(
words, line, boost::is_any_of(" "), boost::algorithm::token_compress_on);
int& r = getReg(words[0]);
int val = stoi(words[2]);
std::cout << "R: " << words[0] << ", op: " << words[1] << ", val: " << val;
if (words.size() > 3 && !eval_cond(words)) {
std::cout << " skip\n";
continue;
}
if (words[1] == "inc") {
std::cout << ", INC\n";
r += val;
} else if (words[1] == "dec") {
std::cout << ", DEC\n";
r -= val;
} else {
std::cerr << "UNKNOWN OP: " << words[1] << "\n";
exit(1);
}
mm = std::max(mm, r);
}
auto m = std::max_element(
reg.begin(), reg.end(), [](const Registers::value_type& a, const Registers::value_type& b) {
return std::less<int>()(a.second, b.second);
});
std::cout << "max: " << m->second << ", mm: " << mm << "\n";
return 0;
}