This commit is contained in:
David Vereb 2024-12-02 17:47:56 -05:00
parent f7083ef726
commit 40c9bd5e50

View File

@ -5,6 +5,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
bool GroupSafe(const std::vector<long> &group);
int main() int main()
{ {
std::ifstream ifs("data.txt"); std::ifstream ifs("data.txt");
@ -41,15 +43,104 @@ int main()
} }
// DEBUG: // DEBUG:
for(auto row : data) // for(auto row : data)
{ // {
for(auto col : row) // for(auto col : row)
std::cout << col << ", "; // std::cout << col << " ";
std::cout << std::endl; // std::cout << std::endl;
} // }
std::cout << " Total: " << total << std::endl; for(auto group : data)
std::cout << "PT2 Total: " << total_pt2 << std::endl; {
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<long> 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; return 0;
} }
bool GroupSafe(const std::vector<long> &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;
}