blob: 47440f98993088382a44bdb87f42ec332668dbc8 (
plain)
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
|
#include <filesystem>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
namespace {
using Graph = map<string, set<string>>;
vector<string>
split(const string& str, const string& delims)
{
vector<string> result;
size_t start = str.find_first_not_of(delims);
while ( start != string::npos ) {
size_t end = str.find_first_of(delims, start);
if ( end == string::npos ) {
// letzter Teil
result.push_back(str.substr(start));
break;
}
result.push_back(str.substr(start, end - start));
start = str.find_first_not_of(delims, end);
}
return result;
}
Graph
read_file(const filesystem::path& filename)
{
ifstream file{ filename };
Graph graph;
for ( string line; getline(file, line); ) {
auto parts = split(line, ": ");
graph[parts[0]].insert(parts.begin() + 1, parts.end());
}
return graph;
}
void
part1(const Graph& graph)
{
map<string, long> cache;
function<long(const string&)> dfs = [&](const string& src) {
if ( src == "out" ) {
return 1L;
}
if ( cache.contains(src) ) {
return cache.at(src);
}
long count = 0;
for ( const auto& node: graph.at(src) ) {
count += dfs(node);
}
return cache[src] = count;
};
cout << "Part 1: " << dfs("you") << '\n';
}
} // namespace
int
main()
{
auto graph = read_file("data/day11.txt");
part1(graph);
}
|