diff --git a/2017/02.cpp b/2017/02.cpp new file mode 100644 index 0000000..b97c263 --- /dev/null +++ b/2017/02.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +namespace { + +auto Omnibus(std::istream & in) +-> std::pair +{ + std::string line; + std::vector numbers; + std::int64_t p1{0}, p2{0}; + + while (std::getline(in, line)) { + numbers.clear(); + + std::istringstream inrow {std::move(line)}; + std::copy( + std::istream_iterator{inrow}, + std::istream_iterator{}, + std::back_inserter(numbers)); + + std::sort(numbers.begin(), numbers.end()); + + p1 += numbers.back() - numbers.front(); + + auto n = numbers.size(); + for (std::size_t i = 0; i < n; i++) { + for (std::size_t j = i+1; j < n; j++) { + if (numbers[j] % numbers[i] == 0) { + p2 += numbers[j] / numbers[i]; + goto found; + } + } + } + found: {} + } + return {p1, p2}; +} + +} // namespace + +TEST_SUITE("2017-02 examples") { + TEST_CASE("part 1") { + std::istringstream in { + "5 1 9 5\n" + "7 5 3\n" + "2 4 6 8\n"}; + CHECK(Omnibus(in).first == 18); + } + TEST_CASE("part 2") { + std::istringstream in { + "5 9 2 8\n" + "9 4 7 3\n" + "3 8 6 5\n"}; + CHECK(Omnibus(in).second == 9); + } +} + +auto main(int argc, char** argv) -> int { + auto in_ptr = aocpp::Startup(argc, argv); + auto & in = *in_ptr; + auto [p1,p2] = Omnibus(in); + std::cout << "Part 1: " << p1 << std::endl; + std::cout << "Part 2: " << p2 << std::endl; +} diff --git a/2017/CMakeLists.txt b/2017/CMakeLists.txt index 6533e18..1bee5ab 100644 --- a/2017/CMakeLists.txt +++ b/2017/CMakeLists.txt @@ -1,3 +1,9 @@ +add_executable(2017_01 01.cpp) +target_link_libraries(2017_01 aocpp) + +add_executable(2017_02 02.cpp) +target_link_libraries(2017_02 aocpp) + add_executable(2017_05 05.cpp) target_link_libraries(2017_05 aocpp)