From 756f22d58bb198b8f34589c112e1003614ccdcd6 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 2 Nov 2025 21:41:58 +0100 Subject: aoc 2017, days 1-20 --- 2017/src/day12.cpp | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 2017/src/day12.cpp (limited to '2017/src/day12.cpp') diff --git a/2017/src/day12.cpp b/2017/src/day12.cpp new file mode 100644 index 0000000..66bf186 --- /dev/null +++ b/2017/src/day12.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +namespace { + +using graph_type = map>; + +vector +split_to_int(const string& line, const string& delimiters) +{ + vector result; + + size_t start = 0; + size_t end = 0; + + while ( (end = line.find_first_of(delimiters, start)) != string::npos ) { + if ( end != start ) { + result.emplace_back(stoi(line.substr(start, end - start))); + } + + start = end + 1; + } + + if ( start != line.size() ) { + result.emplace_back(stoi(line.substr(start))); + } + + return result; +} + +graph_type +read_lines(const filesystem::path& filename) +{ + ifstream file{ filename }; + graph_type result; + + for ( string line; getline(file, line); ) { + auto parts = split_to_int(line, "<-> ,"); + + for ( size_t i = 1; i < parts.size(); ++i ) { + result[parts[0]].insert(parts[i]); + result[parts[i]].insert(parts[0]); + } + } + + return result; +} + +void +bfs_mark(const graph_type& graph, int start, set& seen) +{ + queue queue; + + queue.emplace(start); + seen.emplace(start); + + while ( !queue.empty() ) { + int prg = queue.front(); + queue.pop(); + + for ( auto link: graph.at(prg) ) { + if ( !seen.contains(link) ) { + queue.emplace(link); + seen.emplace(link); + } + } + } +} + +void +part1(const graph_type& graph) +{ + set seen; + + bfs_mark(graph, 0, seen); + + cout << "Part1: " << seen.size() << '\n'; +} + +void +part2(const graph_type& graph) +{ + set seen; + + int count = 0; + + for ( const auto& [i, _]: graph ) { + if ( seen.contains(i) ) { + continue; + } + + ++count; + + bfs_mark(graph, i, seen); + } + cout << "Part2: " << count << '\n'; +} + +} // namespace + +int +main() +{ + auto data = read_lines("data/day12.txt"); + part1(data); + part2(data); +} -- cgit v1.3