diff options
| author | Thomas Schmucker <ts@its1.de> | 2025-12-11 09:34:52 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2025-12-11 09:34:52 +0100 |
| commit | 488b97ea6cda40576c4494e9e32c3b7566ee324a (patch) | |
| tree | d970eaf60d6266f3c1ba14d6e8b1fca3b6c5d7d1 /2025/src | |
| parent | e8bdb4f2ddb1a5137ecbfbe528f40c1cd253df17 (diff) | |
| download | advent-of-code-488b97ea6cda40576c4494e9e32c3b7566ee324a.tar.gz advent-of-code-488b97ea6cda40576c4494e9e32c3b7566ee324a.tar.bz2 advent-of-code-488b97ea6cda40576c4494e9e32c3b7566ee324a.zip | |
aoc 2025, day 11, part 1
Diffstat (limited to '2025/src')
| -rw-r--r-- | 2025/src/day11.cpp | 85 |
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 | |||
| 10 | using namespace std; | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | using Graph = map<string, set<string>>; | ||
| 15 | |||
| 16 | vector<string> | ||
| 17 | split(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 | |||
| 39 | Graph | ||
| 40 | read_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 | |||
| 52 | void | ||
| 53 | part1(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 | |||
| 80 | int | ||
| 81 | main() | ||
| 82 | { | ||
| 83 | auto graph = read_file("data/day11.txt"); | ||
| 84 | part1(graph); | ||
| 85 | } | ||
