aocpp/2022/04.cpp
2022-12-04 09:22:25 -08:00

56 lines
1.1 KiB
C++

#include <cstddef>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <tuple>
#include <doctest.h>
#include <aocpp/Startup.hpp>
namespace {
auto Solve(std::istream & in) -> std::pair<std::size_t, std::size_t>
{
std::size_t p1{0}, p2{0};
std::string line;
while (std::getline(in, line)) {
unsigned a,b,c,d;
if (4 != std::sscanf(line.c_str(), "%u-%u,%u-%u\n", &a, &b, &c, &d)) {
throw std::runtime_error{"bad input"};
}
if (a <= c && d <= b || c <= a && b <= d) p1++;
if (a <= c && c <= b || a <= d && d <= b ||
c <= a && a <= d || c <= b && b <= d) p2++;
}
return {p1, p2};
}
} // namespace
TEST_SUITE("2022-04 examples") {
TEST_CASE("example") {
std::istringstream in {
"2-4,6-8\n"
"2-3,4-5\n"
"5-7,7-9\n"
"2-8,3-7\n"
"6-6,4-6\n"
"2-6,4-8\n"};
auto [p1,p2] = Solve(in);
CHECK(p1 == 2);
CHECK(p2 == 4);
}
}
auto main(int argc, char** argv) -> int {
auto [p1,p2] = Solve(*aocpp::Startup(argc, argv));
std::cout << "Part 1: " << p1 << std::endl;
std::cout << "Part 2: " << p2 << std::endl;
}