#include #include #include #include #include #include #include #include namespace { auto Parse(std::istream & in) -> std::pair { std::int64_t lo, hi; std::string line; std::getline(in, line, '-'); lo = std::stol(std::move(line)); std::getline(in, line); hi = std::stol(std::move(line)); return {lo,hi}; } auto Valid1(std::string const& str) { for (std::size_t i = 1; i < str.size(); i++) { if (str[i-1] == str[i]) { return true; } } return false; } auto Valid2(std::string const& str) { for (std::size_t i = 1; i < str.size(); i++) { if ( str[i] == str[i-1] && (i == str.size() - 1 || str[i] != str[i+1]) && (i == 1 || str[i] != str[i-2])) { return true; } } return false; } } // namespace auto Main(std::istream & in) -> void { auto const [lo,hi] = Parse(in); std::int64_t part1 = 0; std::int64_t part2 = 0; for (auto i = lo; i <= hi; i++) { auto str = std::to_string(i); if (std::is_sorted(str.begin(), str.end())) { if (Valid1(str)) part1++; if (Valid2(str)) part2++; } } std::cout << "Part 1: " << part1 << std::endl; std::cout << "Part 2: " << part2 << std::endl; }