-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
82 lines (72 loc) · 2.41 KB
/
main.cpp
File metadata and controls
82 lines (72 loc) · 2.41 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <chrono>
#include <regex>
#include <unordered_set>
#include "../Utils/utils.h"
std::map<std::string, std::vector<std::string>> paths;
int findPaths(const std::string& curr, std::string visitedCaves, bool lowerCaseTwice = false, int stage = 1){
if(curr == "end"){
return 1;
}
if(curr == "start" && !visitedCaves.empty()){
return 0;
}
bool isLower = std::all_of(curr.begin(), curr.end(), [](char ch) {
return std::islower(static_cast<unsigned char>(ch));
});
if(isLower && visitedCaves.find(curr + ",") != std::string::npos){
if(stage == 1 || lowerCaseTwice){
return 0;
}
lowerCaseTwice = true;
}
visitedCaves += curr + ",";
const std::vector<std::string>& nextMoves = paths[curr];
int possiblePaths = 0;
for(const auto& nextMove : nextMoves){
possiblePaths += findPaths(nextMove, visitedCaves, lowerCaseTwice, stage);
}
return possiblePaths;
}
void printPaths() {
for (const auto& [key, values] : paths) {
std::cout << key << ": ";
for (size_t i = 0; i < values.size(); ++i) {
std::cout << values[i];
if (i < values.size() - 1) { // Add a comma if it's not the last value
std::cout << ", ";
}
}
std::cout << "\n";
}
}
void stage1() {
std::cout << findPaths("start", "") << "\n";
}
void stage2(){
std::cout << findPaths("start", "", false, 2) << "\n";
}
int main(){
std::vector<std::string> puzzle;
std::string input = "example";
input = "input";
aoc_utils::load_puzzle(input, puzzle);
std::vector<std::string> curr;
curr.resize(2);
for(auto& line : puzzle){
curr.clear();
aoc_utils::splitString(line, '-', curr);
paths[curr[0]].push_back(curr[1]);
paths[curr[1]].push_back(curr[0]);
}
auto start = std::chrono::high_resolution_clock::now();
stage1();
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
std::cout << "Stage 1 took " << elapsed.count() << " seconds\n";
start = std::chrono::high_resolution_clock::now();
stage2();
end = std::chrono::high_resolution_clock::now();
elapsed = end - start;
std::cout << "Stage 2 took " << elapsed.count() << " seconds\n";
}