From 40c9bd5e50a65e69f67cf8dd0cc5168823e61620 Mon Sep 17 00:00:00 2001 From: David Vereb Date: Mon, 2 Dec 2024 17:47:56 -0500 Subject: [PATCH] WIP --- 2024/2/main_pt2.cpp | 107 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 99 insertions(+), 8 deletions(-) diff --git a/2024/2/main_pt2.cpp b/2024/2/main_pt2.cpp index 2ba2941..879c660 100644 --- a/2024/2/main_pt2.cpp +++ b/2024/2/main_pt2.cpp @@ -5,6 +5,8 @@ #include #include +bool GroupSafe(const std::vector &group); + int main() { std::ifstream ifs("data.txt"); @@ -41,15 +43,104 @@ int main() } // DEBUG: - for(auto row : data) - { - for(auto col : row) - std::cout << col << ", "; - std::cout << std::endl; - } + // for(auto row : data) + // { + // for(auto col : row) + // std::cout << col << " "; + // std::cout << std::endl; + // } - std::cout << " Total: " << total << std::endl; - std::cout << "PT2 Total: " << total_pt2 << std::endl; + for(auto group : data) + { + switch(group.size()) + { + case 0: + // Go to next line + continue; + break; + case 1: + // Can't be increasing or decreasing because it's the only one, so I guess safe? + ++total; + ++total_pt2; + continue; + break; + }; + + bool safe = true; + + if(GroupSafe(group)) + ++total; + else + { + bool re_safe = false; + for(auto i = 0; i < group.size(); ++i) + { + std::vector check; + if(i != 0) + check = { group.begin(), group.begin() + i - 1 }; + if(i < group.size() - 1) + check.insert(check.end(), group.begin() + i + 1, group.end()); + if(GroupSafe(check)) + { + re_safe = true; + ++total_pt2; + break; + } + } + if(!re_safe) + { + std::cout << "Bad: "; + for(auto val : group) + std::cout << val << " "; + std::cout << std::endl; + } + } + } + std::cout << " Total: " << total << std::endl; + std::cout << "PT2 Total: " << total + total_pt2 << std::endl; return 0; } + + +bool GroupSafe(const std::vector &group) +{ + bool increasing = false; + for(auto i = 0; i < group.size() - 1; ++i) + { + if(group[i] == group[i+1]) + continue; + increasing = group[i] < group[i+1]; + break; + } + + bool safe = true; + int *last = nullptr; + for(auto i = 0; i < group.size(); ++i) + { + auto next = group[i]; + + if(!last) + { + last = new int(next); + continue; + } + + if(increasing && + (next - *last < 1 || next - *last > 3)) + safe = false; + else if(!increasing && + (next - *last < -3 || next - *last > -1)) + safe = false; + + if(!safe) + break; + + *last = next; + } + + if(last) + delete last, last = nullptr; + + return safe; +}