-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path07.cpp
139 lines (114 loc) · 3.37 KB
/
07.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <algorithm>
#include <chrono>
#include <exception>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using std::string;
using std::unordered_map;
using std::vector;
struct Program {
string name;
int weight;
vector<string> childnames;
};
unordered_map<string, Program> parse_input() {
unordered_map<string, Program> programs;
std::string input;
while (std::getline(std::cin, input)) {
auto s = input.find_first_of(' ');
Program p = {input.substr(0, s), std::stoi(&input[s + 2]), {}};
size_t childstart = input.find(" -> ");
if (childstart != string::npos) {
const auto done = input.end();
auto start =
input.begin() + static_cast<string::difference_type>(childstart) + 4;
while (true) {
auto end = std::find_if(start, done, [](char ch) { return ch == ','; });
p.childnames.push_back(string(start, end));
if (end == done) {
break;
}
start = end + 2;
}
}
programs.insert({p.name, p});
}
return programs;
}
// Program is a root if has children
// and is not referenced by any other program as one of its children
bool is_root(const unordered_map<string, Program> &programs,
const string &name) {
for (const auto &[_, p] : programs) {
for (const string &childname : p.childnames) {
if (childname == name) {
return false;
}
}
}
return true;
}
int sum_weight(const unordered_map<string, Program> &programs,
const Program &root) {
int sum = root.weight;
for (const string &childname : root.childnames) {
const Program &child = programs.at(childname);
sum += sum_weight(programs, child);
}
return sum;
}
const Program &find_root(unordered_map<string, Program> &programs) {
for (const auto &[_, a] : programs) {
// skip all leafs
if (a.childnames.size() == 0) {
continue;
}
// potential root, check if referenced from any other program
if (is_root(programs, a.name)) {
return a;
}
}
throw new std::exception();
}
int solve_pt2(const unordered_map<string, Program> &programs,
const Program &root, int unbalanced) {
vector<int> weights;
for (const string &c : root.childnames) {
weights.push_back(sum_weight(programs, programs.at(c)));
}
int diff = 0;
for (unsigned int i = 0; i < root.childnames.size(); i++) {
for (unsigned int j = 0; j < root.childnames.size(); j++) {
if (i == j) {
continue;
}
diff = weights[i] - weights[j];
if (diff == 0) {
break;
}
}
if (diff != 0) {
return solve_pt2(programs, programs.at(root.childnames[i]), diff);
}
}
return root.weight - unbalanced;
}
int main() {
auto tstart = std::chrono::high_resolution_clock::now();
unordered_map<string, Program> programs = parse_input();
const Program &root = find_root(programs);
string pt1 = root.name;
int pt2 = solve_pt2(programs, root, 0);
std::cout << "--- Day 7: Recursive Circus ---\n";
std::cout << "Part 1: " << pt1 << "\n";
std::cout << "Part 2: " << pt2 << "\n";
auto tstop = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(tstop - tstart);
std::cout << "Time: " << (static_cast<double>(duration.count()) / 1000.0)
<< " ms"
<< "\n";
return EXIT_SUCCESS;
}