From dc37b7b4502df98d26092474c5e4d9c4ff119b2e Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Sun, 4 Dec 2022 09:22:25 -0800 Subject: [PATCH] 2022-04 --- 2022/04.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++ 2022/CMakeLists.txt | 3 +++ 2 files changed, 58 insertions(+) create mode 100644 2022/04.cpp diff --git a/2022/04.cpp b/2022/04.cpp new file mode 100644 index 0000000..2d93bae --- /dev/null +++ b/2022/04.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +namespace { + +auto Solve(std::istream & in) -> std::pair +{ + 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; +} diff --git a/2022/CMakeLists.txt b/2022/CMakeLists.txt index d4d59db..96e23bb 100644 --- a/2022/CMakeLists.txt +++ b/2022/CMakeLists.txt @@ -1,2 +1,5 @@ add_executable(2022_01 01.cpp) target_link_libraries(2022_01 aocpp) + +add_executable(2022_04 04.cpp) +target_link_libraries(2022_04 aocpp)