- cross-posted to:
- [email protected]
- [email protected]
- cross-posted to:
- [email protected]
- [email protected]
copy pasting the rules from last year’s thread:
Rules: no spoilers.
The other rules are made up aswe go along.
Share code by link to a forge, home page, pastebin (Eric Wastl has one here) or code section in a comment.
re: 2-2
I was convinced that some of the Perl gods in the subreddit would reveal some forgotten lore that solved this in one line but looks like my brute force method of removing one element at a time was the way to go.
I am now less sleep deprived so can say how to do better somewhat sensibly, albeit I cannot completely escape from C++s verbosity:
2-2
Updated code:
2-2
#include <string> #include <iostream> #include <sstream> #include <vector> #include <iterator> bool valid_pair(const std::vector<int> &arr, int i, int j, bool direction) { if (i < 0) return true; if (j == arr.size()) return true; return !(arr[i] == arr[j]) && (direction ? arr[i] < arr[j] : arr[j] < arr[i]) && (std::abs(arr[j]-arr[i]) <= 3); } bool valid(const std::vector<int> &arr, bool direction) { int checks = 1; for (int i = 1; i < arr.size(); ++i) { if (valid_pair(arr, i-1, i, direction)) continue; if (checks == 0) return false; if ( valid_pair(arr, i-2, i, direction) && valid_pair(arr, i, i+1, direction)) { checks -= 1; i += 1; } else if (valid_pair(arr, i-1, i+1, direction)) { checks -= 1; i += 1; } else return false; } return true; } int main() { int safe = 0; std::string s; while (std::getline(std::cin, s)) { std::istringstream iss(s); std::vector<int> report((std::istream_iterator<int>(iss)), std::istream_iterator<int>()); safe += (valid(report, true) || valid(report, false)); } std::cout << safe << std::endl; }