-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday21.cpp
More file actions
81 lines (70 loc) · 2.25 KB
/
Copy pathday21.cpp
File metadata and controls
81 lines (70 loc) · 2.25 KB
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
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <unordered_map>
#include <utility>
#include<sstream>
using namespace std;
struct Node;
unordered_map<string, shared_ptr<Node>> nodes;
struct Node {
const string name;
string left;
string right;
string op;
int64_t val = 0;
bool marker;
explicit Node(const string& name) : name(name), marker(name == "humn") {}
pair<int64_t, bool> eval() {
if (op.empty()) return make_pair(val, marker);
auto l = nodes[left]->eval();
auto r = nodes[right]->eval();
if (l.second || r.second) marker = true;
switch (op[0]) {
case '+': val = l.first + r.first; break;
case '*': val = l.first * r.first; break;
case '/': val = l.first / r.first; break;
case '-': val = l.first - r.first; break;
}
return make_pair(val, marker);
}
void eval2(int64_t target) {
if (name == "humn") {
cout << "Answer2: " << target << endl;
return;
}
auto fst = nodes[left];
auto snd = nodes[right];
if (fst->marker) swap(fst, snd);
switch (op[0]) {
case '+': snd->eval2(target - fst->val); return;
case '*': snd->eval2(target / fst->val); return;
case '/': left == fst->name ? snd->eval2(fst->val / target) : snd->eval2(fst->val * target); return;
case '-': left == fst->name ? snd->eval2(fst->val - target) : snd->eval2(fst->val + target); return;
}
}
};
int main() {
ifstream infile("day21/day21.in");
string line;
while (getline(infile, line)) {
size_t i = line.find(':');
string node_name = line.substr(0, i);
i += 2;
string expr = line.substr(i);
shared_ptr<Node> ptr(new Node(node_name));
if (expr[0] >= '0' && expr[0] <= '9') {
ptr->val = stoi(expr);
} else {
istringstream ss(expr);
getline(ss, ptr->left, ' ');
getline(ss, ptr->op, ' ');
getline(ss, ptr->right, ' ');
}
nodes[node_name] = ptr;
}
cout << "Answer1: " << nodes["root"]->eval().first << endl;
nodes["root"]->op = "-";
nodes["root"]->eval2(0);
}