aocpp/2022/18.cpp

105 lines
2.1 KiB
C++
Raw Normal View History

2023-01-19 17:13:42 -08:00
#include <algorithm>
#include <concepts>
#include <cstdint>
#include <iostream>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include <doctest.h>
#include <aocpp/Startup.hpp>
#include <aocpp/Vec.hpp>
namespace {
using Coord = aocpp::Vec<std::int64_t, 3>;
/// @brief Parse an input stream
/// @param in white-space delimited, comma-separated coordinates
/// @return set of coordinates
auto Parse(std::istream & in) -> std::set<Coord>
{
std::set<Coord> result;
std::string line;
while (in >> line) {
Coord x;
if (3 != std::sscanf(line.c_str(), "%lld,%lld,%lld\n", &x[0], &x[1], &x[2]))
throw std::runtime_error{"bad input line"};
result.insert(x);
}
return result;
}
auto Part1(std::set<Coord> const& obj) -> std::size_t
{
std::size_t result {0};
for (auto && x : obj) {
x.EachNeighbor([&](Coord const& y) {
if (!obj.contains(y)) result++;
});
}
return result;
}
auto Part2(std::set<Coord> const& obj) -> std::size_t
{
auto [lo, hi] = Coord::BoundingBox(obj.begin(), obj.end());
lo -= Coord(1);
hi += Coord(1);
std::set<Coord> seen {lo};
std::vector<Coord> work {lo};
std::size_t result {0};
while (!work.empty()) {
auto x = work.back();
work.pop_back();
x.EachNeighbor([&, lo=lo, hi=hi](Coord const& y) {
if (obj.contains(y)) {
result++;
} else if (y.InRange(lo, hi) && seen.insert(y).second) {
work.push_back(y);
}
});
}
return result;
}
} // namespace
TEST_SUITE("2022-12 examples") {
TEST_CASE("documented example") {
std::istringstream in {
"2,2,2\n"
"1,2,2\n"
"3,2,2\n"
"2,1,2\n"
"2,3,2\n"
"2,2,1\n"
"2,2,3\n"
"2,2,4\n"
"2,2,6\n"
"1,2,5\n"
"3,2,5\n"
"2,1,5\n"
"2,3,5\n"
};
auto obj = Parse(in);
CHECK(64 == Part1(obj));
CHECK(58 == Part2(obj));
}
}
2023-01-31 08:58:42 -08:00
auto Main(std::istream & in) -> void
2023-01-19 17:13:42 -08:00
{
2023-01-31 08:58:42 -08:00
auto const obj {Parse(in)};
2023-01-19 17:13:42 -08:00
std::cout << "Part 1: " << Part1(obj) << std::endl;
std::cout << "Part 2: " << Part2(obj) << std::endl;
}