aboutsummaryrefslogtreecommitdiff
path: root/2024/src/day23.cpp
blob: 45d6d2859cc1fa5133f3b67ebcf3770bd26aa23a (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
#include <fstream>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

vector<string>
split(const string& line, char sep = ' ')
{
	vector<string> parts;
	stringstream   input{ line };

	for ( string part; getline(input, part, sep); ) {
		parts.emplace_back(part);
	}

	return parts;
}

map<string, set<string>>
read_file(string_view filename)
{
	fstream                  input{ filename };
	map<string, set<string>> data;

	for ( string line; getline(input, line); ) {
		const auto  parts = split(line, '-');
		const auto& lhs   = parts[0];
		const auto& rhs   = parts[1];
		data[lhs].insert(rhs);
		data[rhs].insert(lhs);
	}

	return data;
}

void
part1(const map<string, set<string>>& nodes)
{
	set<set<string>> connected;

	for ( const auto& [node0, links]: nodes ) {
		for ( const auto& node1: links ) {
			for ( const auto& node2: nodes.at(node1) ) {
				if ( node0 != node2 and nodes.at(node2).contains(node0) ) {
					if ( node0.starts_with('t') || node1.starts_with('t') || node2.starts_with('t') ) {
						connected.insert({ node0, node1, node2 });
					}
				}
			}
		}
	}

	cout << connected.size() << endl;
}

int
main()
{
	auto data = read_file("data/day23.txt");
	part1(data);
}