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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
namespace {
vector<string>
split(const string& line)
{
stringstream strm{ line };
vector<string> words;
for ( string word; getline(strm, word, ' '); ) {
words.emplace_back(word);
}
return words;
}
map<string, set<string>>
read_file(const filesystem::path& filename)
{
ifstream file{ filename };
map<string, set<string>> graph;
for ( string line; getline(file, line); ) {
auto words = split(line);
graph[words.at(1)].emplace(words.at(7));
}
return graph;
}
vector<string>
topologicalSort(const map<string, set<string>>& graph)
{
map<string, int> indegree;
// Eingangsgrade initialisieren
for ( const auto& [node, neighbors]: graph ) {
if ( !indegree.contains(node) ) {
indegree[node] = 0;
}
for ( const auto& neighbor: neighbors ) {
indegree[neighbor]++;
}
}
// Queue mit Knoten, deren Eingangsgrad 0 ist
priority_queue<string, vector<string>, greater<>> queue;
for ( const auto& [node, deg]: indegree ) {
if ( deg == 0 ) {
queue.push(node);
}
}
vector<string> sorted;
// Kahn's Algorithmus
while ( !queue.empty() ) {
string node = queue.top();
queue.pop();
sorted.push_back(node);
if ( !graph.contains(node) ) {
continue;
}
for ( const auto& neighbor: graph.at(node) ) {
indegree[neighbor]--;
if ( indegree[neighbor] == 0 ) {
queue.push(neighbor);
}
}
}
// Prüfen auf Zyklus
if ( sorted.size() != indegree.size() ) {
throw runtime_error("Zyklus im Graph gefunden! Keine topologische Sortierung möglich.");
}
return sorted;
}
void
part1(const map<string, set<string>>& graph)
{
auto sorted = topologicalSort(graph);
cout << "Part 1: ";
ranges::copy(sorted, ostream_iterator<string>(cout));
cout << '\n';
}
void
part2(const map<string, set<string>>& graph, const int workers, const int base)
{
map<string, int> indegree;
// Eingangsgrade initialisieren
for ( const auto& [node, neighbors]: graph ) {
if ( !indegree.contains(node) ) {
indegree[node] = 0;
}
for ( const auto& neighbor: neighbors ) {
indegree[neighbor]++;
}
}
set<string> available;
for ( const auto& [node, deg]: indegree ) {
if ( deg == 0 ) {
available.insert(node);
}
}
set<pair<int, string>> finishing;
int active_workers = 0;
int time = 0;
while ( !indegree.empty() ) {
while ( !finishing.empty() && finishing.begin()->first == time ) {
auto [finish_time, node] = *finishing.begin();
finishing.erase(finishing.begin());
active_workers--;
indegree.erase(node);
if ( !graph.contains(node) ) {
continue;
}
for ( const auto& neighbor: graph.at(node) ) {
indegree[neighbor]--;
if ( indegree[neighbor] == 0 ) {
available.insert(neighbor);
}
}
}
while ( active_workers < workers && !available.empty() ) {
auto node = *available.begin();
available.erase(available.begin());
int duration = base + (node.at(0) - 'A' + 1);
int finish_time = time + duration;
finishing.emplace(finish_time, node);
active_workers++;
}
if ( finishing.empty() ) {
if ( available.empty() ) {
break;
}
else {
time++;
}
}
else {
time = finishing.begin()->first;
}
}
cout << "Part 2: " << time << '\n';
}
} // namespace
int
main()
{
auto graph = read_file("data/day07.txt");
try {
part1(graph);
static const int WORKERS = 5;
static const int BASE = 60;
part2(graph, WORKERS, BASE);
}
catch ( exception& e ) {
cerr << "Fehler: " << e.what() << '\n';
}
}
|