aocpp/2022/14.cpp

178 lines
4.7 KiB
C++
Raw Permalink Normal View History

2023-04-02 14:42:46 -07:00
#include <version>
2023-03-29 12:02:14 -07:00
#include <cstdint>
#include <iostream>
#include <sstream>
2023-04-04 07:23:11 -07:00
#include <stack>
2023-04-07 09:45:55 -07:00
#include <stdexcept>
2023-04-04 07:23:11 -07:00
#include <tuple>
2023-04-04 20:58:59 -07:00
#include <unordered_set>
2023-03-29 12:02:14 -07:00
#include <vector>
#include <boost/phoenix.hpp>
#include <boost/range/irange.hpp>
#include <boost/spirit/include/qi.hpp>
#include <doctest.h>
2023-04-08 12:08:51 -07:00
#include <aocpp/Parsing.hpp>
2023-03-29 12:02:14 -07:00
#include <aocpp/Startup.hpp>
#include <aocpp/Coord.hpp>
namespace {
namespace phx = boost::phoenix;
namespace qi = boost::spirit::qi;
using Input = std::vector<std::vector<aocpp::Coord>>;
const aocpp::Coord TOP {500, 0};
// Input file grammar
2023-04-08 12:08:51 -07:00
class Grammar : public qi::grammar<std::string::const_iterator, Input()> {
qi::rule<iterator_type, aocpp::Coord> coord;
qi::rule<iterator_type, std::vector<aocpp::Coord>()> line;
qi::rule<iterator_type, Input()> input;
2023-03-29 12:02:14 -07:00
public:
Grammar() : Grammar::base_type{input} {
using namespace qi::labels;
coord = qi::long_long [ phx::bind(&aocpp::Coord::x, _val) = _1 ] >>
"," >>
qi::long_long [ phx::bind(&aocpp::Coord::y, _val) = _1 ];
line = coord % " -> " >> "\n";
input = *line;
}
};
2023-04-04 20:58:59 -07:00
auto ExpandLines(Input const& lines) -> std::unordered_set<aocpp::Coord>
2023-03-29 12:02:14 -07:00
{
2023-04-04 20:58:59 -07:00
auto result = std::unordered_set<aocpp::Coord>{};
2023-03-29 12:02:14 -07:00
for (auto const& line : lines) {
for (auto const i : boost::irange(std::size_t{1}, line.size())) {
auto const& a = line[i-1];
auto const& b = line[i];
if (a.x == b.x) {
for (auto const y : boost::irange(std::min(a.y, b.y), 1+std::max(a.y, b.y))) {
result.insert({a.x, y});
}
} else if (a.y == b.y) {
for (auto const x : boost::irange(std::min(a.x, b.x), 1+std::max(a.x, b.x))) {
result.insert({x, a.y});
}
} else {
2023-04-04 08:57:50 -07:00
throw std::runtime_error{"Bad line segment"};
2023-03-29 12:02:14 -07:00
}
}
}
return result;
}
auto FindBottom(Input const& input) -> std::int64_t
{
auto result = std::int64_t{TOP.y};
for (auto const& line : input) {
for (auto const& coord : line) {
result = std::max(result, coord.y);
}
}
return result;
}
2023-04-04 07:23:11 -07:00
enum class Action { Visit, Mark };
2023-04-02 14:42:46 -07:00
2023-04-04 20:58:59 -07:00
auto Part1(std::int64_t bottom, std::unordered_set<aocpp::Coord> world) -> std::size_t
2023-03-29 12:02:14 -07:00
{
auto const starting_size = world.size();
2023-04-04 07:23:11 -07:00
auto work = std::stack<std::pair<Action, aocpp::Coord>>{};
work.emplace(Action::Visit, TOP);
while (!work.empty()) {
2023-04-04 20:58:59 -07:00
auto & [action, here] = work.top();
2023-04-04 07:23:11 -07:00
switch (action) {
case Action::Visit:
if (!world.contains(here)) {
2023-04-04 20:58:59 -07:00
// Particle is falling off the world, stop simulation
2023-04-04 07:23:11 -07:00
if (here.y == bottom) {
return world.size() - starting_size;
}
2023-04-04 20:58:59 -07:00
action = Action::Mark;
auto const down = aocpp::Down(here);
// action and here are invalidated after this line
work.emplace(Action::Visit, aocpp::Right(down));
work.emplace(Action::Visit, aocpp::Left(down));
work.emplace(Action::Visit, down);
} else {
work.pop();
2023-04-02 14:42:46 -07:00
}
2023-04-04 07:23:11 -07:00
break;
case Action::Mark:
2023-04-04 20:58:59 -07:00
work.pop();
2023-04-04 07:23:11 -07:00
world.insert(here);
break;
2023-03-29 12:02:14 -07:00
}
}
2023-04-02 15:30:34 -07:00
throw std::runtime_error{"No solution for part 1"};
2023-03-29 12:02:14 -07:00
}
2023-04-04 20:58:59 -07:00
auto Part2(std::int64_t bottom, std::unordered_set<aocpp::Coord> world) -> std::size_t
2023-03-29 12:02:14 -07:00
{
auto const starting_size = world.size();
2023-04-04 07:23:11 -07:00
auto work = std::stack<std::pair<Action, aocpp::Coord>>{};
work.emplace(Action::Visit, TOP);
while (!work.empty()) {
2023-04-04 20:58:59 -07:00
auto & [action, here] = work.top();
2023-04-04 07:23:11 -07:00
switch (action) {
case Action::Visit:
if (here.y - 2 != bottom && !world.contains(here)) {
2023-04-04 20:58:59 -07:00
action = Action::Mark;
auto const down = aocpp::Down(here);
work.emplace(Action::Visit, aocpp::Right(down));
work.emplace(Action::Visit, aocpp::Left(down));
work.emplace(Action::Visit, down);
} else {
work.pop();
2023-04-04 07:23:11 -07:00
}
break;
case Action::Mark:
world.insert(here);
2023-04-04 20:58:59 -07:00
work.pop();
2023-04-04 07:23:11 -07:00
break;
2023-03-29 12:02:14 -07:00
}
}
return world.size() - starting_size;
}
} // namespace
TEST_SUITE("2022-14 examples") {
TEST_CASE("documented example") {
auto in = std::istringstream{
"498,4 -> 498,6 -> 496,6\n"
"503,4 -> 502,4 -> 502,9 -> 494,9\n"
};
2023-04-08 12:08:51 -07:00
auto const input = aocpp::ParseGrammar(Grammar{}, in);
2023-03-29 12:02:14 -07:00
auto const bottom = FindBottom(input);
auto world = ExpandLines(input);
CHECK(24 == Part1(bottom, world));
CHECK(93 == Part2(bottom, std::move(world)));
}
}
auto Main(std::istream & in, std::ostream & out) -> void
{
2023-04-08 12:08:51 -07:00
auto const input = aocpp::ParseGrammar(Grammar{}, in);
2023-03-29 12:02:14 -07:00
auto const bottom = FindBottom(input);
auto world = ExpandLines(input);
out << "Part 1: " << Part1(bottom, world) << std::endl;
out << "Part 2: " << Part2(bottom, std::move(world)) << std::endl;
}