diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-12-23 10:47:46 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-12-23 10:47:46 +0100 |
| commit | 12ba4504a7bbf3449552e37e8f28920e95b890c0 (patch) | |
| tree | 0e566d3e056c99f84f493e15fe54065f39448d8b /2024/src | |
| parent | cf484b113493ef53b3176d3c3a67d4e0e6fe14ca (diff) | |
| download | advent-of-code-12ba4504a7bbf3449552e37e8f28920e95b890c0.tar.gz advent-of-code-12ba4504a7bbf3449552e37e8f28920e95b890c0.tar.bz2 advent-of-code-12ba4504a7bbf3449552e37e8f28920e95b890c0.zip | |
aoc 2024, day 23, part 1
Diffstat (limited to '2024/src')
| -rw-r--r-- | 2024/src/day23.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/2024/src/day23.cpp b/2024/src/day23.cpp new file mode 100644 index 0000000..45d6d28 --- /dev/null +++ b/2024/src/day23.cpp | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <map> | ||
| 4 | #include <set> | ||
| 5 | #include <sstream> | ||
| 6 | #include <string> | ||
| 7 | #include <vector> | ||
| 8 | using namespace std; | ||
| 9 | |||
| 10 | vector<string> | ||
| 11 | split(const string& line, char sep = ' ') | ||
| 12 | { | ||
| 13 | vector<string> parts; | ||
| 14 | stringstream input{ line }; | ||
| 15 | |||
| 16 | for ( string part; getline(input, part, sep); ) { | ||
| 17 | parts.emplace_back(part); | ||
| 18 | } | ||
| 19 | |||
| 20 | return parts; | ||
| 21 | } | ||
| 22 | |||
| 23 | map<string, set<string>> | ||
| 24 | read_file(string_view filename) | ||
| 25 | { | ||
| 26 | fstream input{ filename }; | ||
| 27 | map<string, set<string>> data; | ||
| 28 | |||
| 29 | for ( string line; getline(input, line); ) { | ||
| 30 | const auto parts = split(line, '-'); | ||
| 31 | const auto& lhs = parts[0]; | ||
| 32 | const auto& rhs = parts[1]; | ||
| 33 | data[lhs].insert(rhs); | ||
| 34 | data[rhs].insert(lhs); | ||
| 35 | } | ||
| 36 | |||
| 37 | return data; | ||
| 38 | } | ||
| 39 | |||
| 40 | void | ||
| 41 | part1(const map<string, set<string>>& nodes) | ||
| 42 | { | ||
| 43 | set<set<string>> connected; | ||
| 44 | |||
| 45 | for ( const auto& [node0, links]: nodes ) { | ||
| 46 | for ( const auto& node1: links ) { | ||
| 47 | for ( const auto& node2: nodes.at(node1) ) { | ||
| 48 | if ( node0 != node2 and nodes.at(node2).contains(node0) ) { | ||
| 49 | if ( node0.starts_with('t') || node1.starts_with('t') || node2.starts_with('t') ) { | ||
| 50 | connected.insert({ node0, node1, node2 }); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | cout << connected.size() << endl; | ||
| 58 | } | ||
| 59 | |||
| 60 | int | ||
| 61 | main() | ||
| 62 | { | ||
| 63 | auto data = read_file("data/day23.txt"); | ||
| 64 | part1(data); | ||
| 65 | } | ||
