aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2025/src/day11.cpp85
1 files changed, 85 insertions, 0 deletions
diff --git a/2025/src/day11.cpp b/2025/src/day11.cpp
new file mode 100644
index 0000000..47440f9
--- /dev/null
+++ b/2025/src/day11.cpp
@@ -0,0 +1,85 @@
1#include <filesystem>
2#include <fstream>
3#include <functional>
4#include <iostream>
5#include <map>
6#include <set>
7#include <string>
8#include <vector>
9
10using namespace std;
11
12namespace {
13
14using Graph = map<string, set<string>>;
15
16vector<string>
17split(const string& str, const string& delims)
18{
19 vector<string> result;
20
21 size_t start = str.find_first_not_of(delims);
22 while ( start != string::npos ) {
23 size_t end = str.find_first_of(delims, start);
24
25 if ( end == string::npos ) {
26 // letzter Teil
27 result.push_back(str.substr(start));
28 break;
29 }
30
31 result.push_back(str.substr(start, end - start));
32
33 start = str.find_first_not_of(delims, end);
34 }
35
36 return result;
37}
38
39Graph
40read_file(const filesystem::path& filename)
41{
42 ifstream file{ filename };
43
44 Graph graph;
45 for ( string line; getline(file, line); ) {
46 auto parts = split(line, ": ");
47 graph[parts[0]].insert(parts.begin() + 1, parts.end());
48 }
49 return graph;
50}
51
52void
53part1(const Graph& graph)
54{
55 map<string, long> cache;
56
57 function<long(const string&)> dfs = [&](const string& src) {
58 if ( src == "out" ) {
59 return 1L;
60 }
61
62 if ( cache.contains(src) ) {
63 return cache.at(src);
64 }
65
66 long count = 0;
67
68 for ( const auto& node: graph.at(src) ) {
69 count += dfs(node);
70 }
71
72 return cache[src] = count;
73 };
74
75 cout << "Part 1: " << dfs("you") << '\n';
76}
77
78} // namespace
79
80int
81main()
82{
83 auto graph = read_file("data/day11.txt");
84 part1(graph);
85}