This commit is contained in:
Eric Mertens 2022-11-28 17:15:13 -08:00
parent afc832f928
commit 3982cb8a4c
2 changed files with 81 additions and 0 deletions

75
2017/02.cpp Normal file
View File

@ -0,0 +1,75 @@
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include <doctest.h>
#include <aocpp/Startup.hpp>
namespace {
auto Omnibus(std::istream & in)
-> std::pair<std::int64_t, std::int64_t>
{
std::string line;
std::vector<std::int64_t> 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<std::int64_t>{inrow},
std::istream_iterator<std::int64_t>{},
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;
}

View File

@ -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)