This commit is contained in:
Eric Mertens 2022-12-04 09:22:25 -08:00
parent d3525c097b
commit dc37b7b450
2 changed files with 58 additions and 0 deletions

55
2022/04.cpp Normal file
View File

@ -0,0 +1,55 @@
#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;
}

View File

@ -1,2 +1,5 @@
add_executable(2022_01 01.cpp) add_executable(2022_01 01.cpp)
target_link_libraries(2022_01 aocpp) target_link_libraries(2022_01 aocpp)
add_executable(2022_04 04.cpp)
target_link_libraries(2022_04 aocpp)