2022-04
This commit is contained in:
parent
d3525c097b
commit
dc37b7b450
55
2022/04.cpp
Normal file
55
2022/04.cpp
Normal 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;
|
||||
}
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user