Fuss with 12
This commit is contained in:
parent
de2117327e
commit
0ebaa66b24
32
2022/12.cpp
32
2022/12.cpp
|
@ -19,10 +19,13 @@ namespace {
|
||||||
|
|
||||||
auto Parse(std::istream & in) -> std::tuple<Coord, std::vector<Coord>, Coord, Grid>
|
auto Parse(std::istream & in) -> std::tuple<Coord, std::vector<Coord>, Coord, Grid>
|
||||||
{
|
{
|
||||||
Coord start1 {};
|
std::tuple<Coord, std::vector<Coord>, Coord, Grid> result {{},{},{},Grid::Parse(in)};
|
||||||
std::vector<Coord> starts2;
|
// I'd use a structured binding here, but not all current compilers
|
||||||
Coord end {};
|
// supports capture of the resulting bindings in the lambda below
|
||||||
auto grid = Grid::Parse(in);
|
auto& start1 = std::get<0>(result);
|
||||||
|
auto& starts2 = std::get<1>(result);
|
||||||
|
auto& end = std::get<2>(result);
|
||||||
|
auto& grid = std::get<3>(result);
|
||||||
|
|
||||||
grid.each([&](Coord c, char v) {
|
grid.each([&](Coord c, char v) {
|
||||||
switch (v) {
|
switch (v) {
|
||||||
|
@ -32,29 +35,28 @@ auto Parse(std::istream & in) -> std::tuple<Coord, std::vector<Coord>, Coord, Gr
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return {start1, std::move(starts2), end, std::move(grid)};
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto Solve(std::vector<Coord> starts, Coord end, Grid const& grid) -> std::optional<std::int64_t>
|
auto Solve(std::vector<Coord> starts, Coord end, Grid const& grid) -> std::optional<std::int64_t>
|
||||||
{
|
{
|
||||||
std::vector<Coord> next_layer;
|
std::vector<Coord> next_layer;
|
||||||
std::set<Coord> seen;
|
std::set<Coord> seen;
|
||||||
std::int64_t counter {1};
|
std::int64_t counter {};
|
||||||
|
|
||||||
while (!starts.empty()) {
|
while (!starts.empty()) {
|
||||||
for (auto here : starts) {
|
counter++;
|
||||||
|
for (auto const& here : starts) {
|
||||||
if (!seen.insert(here).second) continue;
|
if (!seen.insert(here).second) continue;
|
||||||
auto here_height = grid[here];
|
for (auto const next : {Up(here), Down(here), Left(here), Right(here)}) {
|
||||||
for (auto next : {Up(here), Down(here), Left(here), Right(here)}) {
|
if (grid.contains(next) && grid[next] <= grid[here] + 1) {
|
||||||
if (!grid.contains(next)) continue;
|
if (next == end) { return counter; }
|
||||||
if (grid[next] - 1 > here_height) continue;
|
next_layer.push_back(next);
|
||||||
if (next == end) { return counter; }
|
}
|
||||||
next_layer.push_back(next);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
counter++;
|
|
||||||
starts.clear();
|
|
||||||
std::swap(starts, next_layer);
|
std::swap(starts, next_layer);
|
||||||
|
next_layer.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user