aocpp/2019/04.cpp
2023-01-31 09:15:25 -08:00

64 lines
1.3 KiB
C++

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <tuple>
#include <fstream>
#include <iterator>
#include <vector>
#include <aocpp/Startup.hpp>
namespace {
auto Parse(std::istream & in) -> std::pair<std::int64_t, std::int64_t> {
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, std::ostream & out) -> 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++;
}
}
out << "Part 1: " << part1 << std::endl;
out << "Part 2: " << part2 << std::endl;
}