aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2024-12-23 10:47:46 +0100
committerThomas Schmucker <ts@its1.de>2024-12-23 10:47:46 +0100
commit12ba4504a7bbf3449552e37e8f28920e95b890c0 (patch)
tree0e566d3e056c99f84f493e15fe54065f39448d8b
parentcf484b113493ef53b3176d3c3a67d4e0e6fe14ca (diff)
downloadadvent-of-code-12ba4504a7bbf3449552e37e8f28920e95b890c0.tar.gz
advent-of-code-12ba4504a7bbf3449552e37e8f28920e95b890c0.tar.bz2
advent-of-code-12ba4504a7bbf3449552e37e8f28920e95b890c0.zip
aoc 2024, day 23, part 1
-rw-r--r--2024/src/day23.cpp65
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>
8using namespace std;
9
10vector<string>
11split(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
23map<string, set<string>>
24read_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
40void
41part1(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
60int
61main()
62{
63 auto data = read_file("data/day23.txt");
64 part1(data);
65}