-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_21a.cpp
120 lines (111 loc) · 2.96 KB
/
day_21a.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <cassert>
enum class YellType {
NUMBER,
RESULT
};
struct Monkey {
long long id;
std::string name;
YellType type;
long long val = 0;
std::string dep1;
std::string dep2;
char op;
bool calced = false;
};
long long calc_result(long long num1, long long num2, char op) {
if (op == '+') {
return num1 + num2;
}
if (op == '-') {
return num1 - num2;
}
if (op == '*') {
return num1 * num2;
}
if (op == '/') {
// assert (num1 / num2 == float(num1) / float(num2));
return num1 / num2;
}
else {
std::cout << "This should not happen" << '\n';
exit(0);
}
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_21_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::vector<Monkey> monkeys;
std::unordered_map<std::string, long long> monkey_mapping;
long long id = 0;
while(std::getline(file, line)) {
auto m = Monkey();
m.id = id;
m.name = line.substr(0, 4);
// std::cout << m.name << ": " ;
if (line[6] >= '0' && line[6] <= '9') {
m.type = YellType::NUMBER;
m.val = std::stoll(line.substr(6, line.size() - 6));
// std::cout << m.val << '\n';
} else {
m.type = YellType::RESULT;
auto start = 6;
auto end = line.find(' ' , start);
m.dep1 = line.substr(start, end - start);
start = end+1;
m.op = line[start];
start+=2;
m.dep2 = line.substr(start, line.size() - start);
// std::cout << m.dep1 << ' ' << m.op << ' ' << m.dep2 << '\n';
}
m.calced = false;
monkeys.push_back(m);
monkey_mapping[m.name] = id;
id++;
}
long long count = 0;
while (true) {
for (auto& monkey : monkeys) {
// std::cout << "Monkey " << monkey.name << '\n';
if (monkey.calced) continue;
if (monkey.type == YellType::RESULT) {
if (!monkeys[monkey_mapping[monkey.dep1]].calced) {
// std::cout << monkey.dep1 << " has not run its calc yet " << '\n';
continue;
}
if (!monkeys[monkey_mapping[monkey.dep2]].calced) {
// std::cout << monkey.dep2 << " has not run its calc yet " << '\n';
continue;
}
monkey.val = calc_result(
monkeys[monkey_mapping[monkey.dep1]].val,
monkeys[monkey_mapping[monkey.dep2]].val,
monkey.op
);
monkey.calced = true;
// std::cout << monkey.name << " ran its calc " << '\n';
if (monkey.name == "root") {
// std::cout << monkeys[monkey_mapping[monkey.dep1]].val << '\n';
// std::cout << monkeys[monkey_mapping[monkey.dep2]].val << '\n';
std::cout << monkey.val << '\n';
return 0;
}
} else {
// std::cout << monkey.name << " ran its calc " << '\n';
monkey.calced = true;
}
}
count++;
}
return 0;
}