diff --git a/2022/12.cpp b/2022/12.cpp new file mode 100644 index 0000000..f0d9993 --- /dev/null +++ b/2022/12.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +using aocpp::Coord; +using aocpp::Grid; + +namespace { + +auto Parse(std::istream & in) -> std::tuple +{ + Coord start {}; + Coord end {}; + auto grid = Grid::Parse(in); + + grid.each([&](Coord c, char v) { + switch (v) { + case 'S': grid[c] = 'a'; start = c; break; + case 'E': grid[c] = 'z'; end = c; break; + } + }); + + return {start, end, std::move(grid)}; +} + +auto Solve(std::vector starts, Coord end, Grid const& grid) -> std::int64_t +{ + std::vector prev_layer(std::move(starts)); + std::vector next_layer; + std::set seen; + std::int64_t counter {1}; + + while (!prev_layer.empty()) { + for (auto here : prev_layer) { + if (!seen.insert(here).second) continue; + auto here_height = grid[here]; + for (auto next : {Up(here), Down(here), Left(here), Right(here)}) { + if (!grid.contains(next)) continue; + if (grid[next] - here_height > 1) continue; + if (next == end) { return counter; } + next_layer.push_back(next); + } + } + counter++; + prev_layer.clear(); + std::swap(prev_layer, next_layer); + } + + return -1; +} + +auto Part2Starts(Grid const& grid) -> std::vector +{ + std::vector starts; + grid.each([&](Coord c, char v) { + if (v == 'a') starts.push_back(c); + }); + return starts; +} + +} // namespace + +TEST_SUITE("2022-12 examples") { + TEST_CASE("documented example") { + std::istringstream in { + "Sabqponm\n" + "abcryxxl\n" + "accszExk\n" + "acctuvwj\n" + "abdefghi\n" + }; + auto [start, end, grid] = Parse(in); + CHECK(31 == Solve({start}, end, grid)); + CHECK(29 == Solve(Part2Starts(grid), end, grid)); + } +} + +auto main(int argc, char** argv) -> int +{ + auto [start, end, grid] = Parse(*aocpp::Startup(argc, argv)); + std::cout << "Part 1: " << Solve({start}, end, grid) << std::endl; + std::cout << "Part 2: " << Solve(Part2Starts(grid), end, grid) << std::endl; +} diff --git a/2022/CMakeLists.txt b/2022/CMakeLists.txt index a9fe453..76613ed 100644 --- a/2022/CMakeLists.txt +++ b/2022/CMakeLists.txt @@ -7,5 +7,8 @@ target_link_libraries(2022_03 aocpp) add_executable(2022_04 04.cpp) target_link_libraries(2022_04 aocpp) +add_executable(2022_12 12.cpp) +target_link_libraries(2022_12 aocpp) + add_executable(2022_25 25.cpp) target_link_libraries(2022_25 aocpp)