#include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { using Coord = aocpp::Vec; /// @brief Parse an input stream /// @param in white-space delimited, comma-separated coordinates /// @return set of coordinates auto Parse(std::istream & in) -> std::set { std::set 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 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 const& obj) -> std::size_t { auto [lo, hi] = Coord::BoundingBox(obj.begin(), obj.end()); lo -= Coord(1); hi += Coord(1); std::set seen {lo}; std::vector 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)); } } auto Main(std::istream & in) -> void { auto const obj {Parse(in)}; std::cout << "Part 1: " << Part1(obj) << std::endl; std::cout << "Part 2: " << Part2(obj) << std::endl; }