2022-11-28 17:15:13 -08:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-31 08:58:42 -08:00
|
|
|
auto Main(std::istream & in) -> void
|
|
|
|
{
|
|
|
|
auto const [p1,p2] = Omnibus(in);
|
2022-11-28 17:15:13 -08:00
|
|
|
std::cout << "Part 1: " << p1 << std::endl;
|
|
|
|
std::cout << "Part 2: " << p2 << std::endl;
|
|
|
|
}
|