From c432d3f381384f0a07f29dd417dcd30cb227db0d Mon Sep 17 00:00:00 2001 From: Marcus Penate Date: Wed, 7 Dec 2022 09:07:06 -0500 Subject: [PATCH] Day 12 --- Day 12/part1.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++ Day 12/part2.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 Day 12/part1.cpp create mode 100644 Day 12/part2.cpp diff --git a/Day 12/part1.cpp b/Day 12/part1.cpp new file mode 100644 index 0000000..143a45b --- /dev/null +++ b/Day 12/part1.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +string start = "start"; +string finish = "end"; + +string lowercase = "qwertyuiopasdfghjklzxcvbnm"; + +int find_all_paths(unordered_map> graph, vector current_path); + +int main(int argc, char* argv[]) +{ + unordered_map> graph; + + string line; + while(getline(cin, line)) + { + string::size_type n = line.find("-",0); + string node1 = line.substr(0,n); + string node2 = line.substr(n+1); + + if (!graph.contains(node1)) + { + graph.emplace(node1, set()); + } + + if (!graph.contains(node2)) + { + graph.emplace(node2, set()); + } + + graph[node1].insert(node2); + graph[node2].insert(node1); + } + + vector beginning_path; + beginning_path.push_back(start); + + cout << find_all_paths(graph, beginning_path) << endl; +} + +int find_all_paths(unordered_map> graph, vector current_path) +{ + int found_paths = 0; + set connected_nodes = graph[current_path.back()]; + + for (set::iterator it = connected_nodes.begin(); it != connected_nodes.end(); ++it) + { + string current_node = *it; + + if (current_node.compare(finish) == 0) + { + ++found_paths; + } + else if (string::npos == current_node.find_first_of(lowercase) || + current_path.end() == std::find(current_path.begin(), current_path.end(), current_node)) + { //Big cave or small cave not in path + vector next_path(current_path); + next_path.push_back(current_node); + found_paths += find_all_paths(graph, next_path); + } + } + + return found_paths; +} \ No newline at end of file diff --git a/Day 12/part2.cpp b/Day 12/part2.cpp new file mode 100644 index 0000000..65bbdfb --- /dev/null +++ b/Day 12/part2.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +string start = "start"; +string finish = "end"; + +string lowercase = "qwertyuiopasdfghjklzxcvbnm"; + +int find_all_paths(unordered_map> graph, vector current_path, bool double_small_taken); + +int main(int argc, char* argv[]) +{ + unordered_map> graph; + + string line; + while(getline(cin, line)) + { + string::size_type n = line.find("-",0); + string node1 = line.substr(0,n); + string node2 = line.substr(n+1); + + if (!graph.contains(node1)) + { + graph.emplace(node1, set()); + } + + if (!graph.contains(node2)) + { + graph.emplace(node2, set()); + } + + if (0 != node2.compare(start)) + graph[node1].insert(node2); + if (0 != node1.compare(start)) + graph[node2].insert(node1); + } + + vector beginning_path; + beginning_path.push_back(start); + + cout << find_all_paths(graph, beginning_path, false) << endl; +} + +int find_all_paths(unordered_map> graph, vector current_path, bool double_small_taken) +{ + int found_paths = 0; + set connected_nodes = graph[current_path.back()]; + + for (set::iterator it = connected_nodes.begin(); it != connected_nodes.end(); ++it) + { + string current_node = *it; + + if (current_node.compare(finish) == 0) + { + ++found_paths; + } + else if (string::npos == current_node.find_first_of(lowercase) || + current_path.end() == std::find(current_path.begin(), current_path.end(), current_node)) + { //Big cave or small cave not in path + vector next_path(current_path); + next_path.push_back(current_node); + found_paths += find_all_paths(graph, next_path, double_small_taken); + } + else if (!double_small_taken) + { //Small cave and in path, double is not taken though + vector next_path(current_path); + next_path.push_back(current_node); + found_paths += find_all_paths(graph, next_path, true); + } + } + + return found_paths; +} \ No newline at end of file