aocpp/2019/04.cpp

63 lines
1.3 KiB
C++
Raw Normal View History

2022-11-07 21:00:14 -08:00
#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(int argc, char** argv) -> int {
auto [lo,hi] = Parse(aocpp::Startup(argc, argv));
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;
}