-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_08.cpp
48 lines (42 loc) · 1.23 KB
/
day_08.cpp
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
// Advent of Code 2017
// http://adventofcode.com/
// Day 08 - I Heard You Like Registers
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;
/*
m n o p q r s
b inc 5 if a > 1
a inc 1 if b < 5
c dec -10 if a >= 1
c inc -20 if c == 10
*/
void step(map<string, function<bool(int, int)> > ops, map<string, int>& reg, map<string, int>& regmax, stringstream ins)
{
string m, n, p, q, r; int o, s;
ins >> m >> n >> o >> p >> q >> r >> s;
if (ops[r](reg[q],s))
reg[m] += o*(n == "inc" ? 1 : -1);
regmax[m] = max(reg[m], regmax[m]);
}
int main(int argc, char* argv[])
{
map<string, function<bool(int, int)> > ops;
ops["<"] = [](int a, int b)->bool {return a < b; };
ops[">"] = [](int a, int b)->bool {return a > b; };
ops["<="] = [](int a, int b)->bool {return a <= b; };
ops[">="] = [](int a, int b)->bool {return a >= b; };
ops["=="] = [](int a, int b)->bool {return a == b; };
ops["!="] = [](int a, int b)->bool {return a != b; };
map<string, int> reg, regmax;
string row;
while (getline(cin, row))
step(ops, reg, regmax, stringstream(row));
return 0;
}