2023-01-19 17:13:42 -08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <concepts>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iostream>
|
|
|
|
#include <set>
|
|
|
|
#include <sstream>
|
2023-04-07 09:45:55 -07:00
|
|
|
#include <stack>
|
2023-01-19 17:13:42 -08:00
|
|
|
#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>
|
|
|
|
{
|
2023-04-07 09:45:55 -07:00
|
|
|
auto result = std::set<Coord>{};
|
|
|
|
auto line = std::string{};
|
|
|
|
|
2023-01-19 17:13:42 -08:00
|
|
|
while (in >> line) {
|
2023-04-07 09:45:55 -07:00
|
|
|
auto x = Coord{};
|
|
|
|
if (3 != std::sscanf(line.c_str(), "%lld,%lld,%lld\n", &x[0], &x[1], &x[2])) {
|
2023-01-19 17:13:42 -08:00
|
|
|
throw std::runtime_error{"bad input line"};
|
2023-04-07 09:45:55 -07:00
|
|
|
}
|
2023-01-19 17:13:42 -08:00
|
|
|
result.insert(x);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Part1(std::set<Coord> const& obj) -> std::size_t
|
|
|
|
{
|
2023-04-07 09:45:55 -07:00
|
|
|
auto result = std::size_t{0};
|
|
|
|
for (auto const& x : obj) {
|
2023-01-19 17:13:42 -08:00
|
|
|
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());
|
2023-04-07 09:45:55 -07:00
|
|
|
lo -= Coord{1};
|
|
|
|
hi += Coord{1};
|
2023-01-19 17:13:42 -08:00
|
|
|
|
2023-04-07 09:45:55 -07:00
|
|
|
auto seen = std::set<Coord>{};
|
|
|
|
auto work = std::stack<Coord>{};
|
|
|
|
auto result = std::size_t{0};
|
|
|
|
|
|
|
|
work.push(lo);
|
|
|
|
seen.insert(lo);
|
2023-01-19 17:13:42 -08:00
|
|
|
|
|
|
|
while (!work.empty()) {
|
2023-04-07 09:45:55 -07:00
|
|
|
auto const x = work.top();
|
|
|
|
work.pop();
|
2023-01-19 17:13:42 -08:00
|
|
|
x.EachNeighbor([&, lo=lo, hi=hi](Coord const& y) {
|
|
|
|
if (obj.contains(y)) {
|
|
|
|
result++;
|
|
|
|
} else if (y.InRange(lo, hi) && seen.insert(y).second) {
|
2023-04-07 09:45:55 -07:00
|
|
|
work.push(y);
|
2023-01-19 17:13:42 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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 09:15:15 -08:00
|
|
|
auto Main(std::istream & in, std::ostream & out) -> void
|
2023-01-19 17:13:42 -08:00
|
|
|
{
|
2023-04-07 09:45:55 -07:00
|
|
|
auto const obj = Parse(in);
|
2023-01-31 09:15:15 -08:00
|
|
|
out << "Part 1: " << Part1(obj) << std::endl;
|
|
|
|
out << "Part 2: " << Part2(obj) << std::endl;
|
2023-01-19 17:13:42 -08:00
|
|
|
}
|