Make Grid iterable
This commit is contained in:
12
2019/10.cpp
12
2019/10.cpp
@@ -70,23 +70,23 @@ auto ClearView(Grid const& grid, Coord src, Coord dst) -> bool {
|
||||
auto Part1(Grid const& grid) {
|
||||
std::size_t best = 0;
|
||||
Coord base {};
|
||||
grid.each([&](Coord src, char s) {
|
||||
for (auto [src, s] : grid) {
|
||||
if ('#' == s) {
|
||||
std::size_t visible = 0;
|
||||
grid.each([&](Coord dst, char d){
|
||||
for (auto [dst, d] : grid) {
|
||||
if ('#' == d && src != dst) {
|
||||
if (ClearView(grid, src, dst)) {
|
||||
visible++;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (visible > best) {
|
||||
best = visible;
|
||||
base = src;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return std::make_pair(best, base);
|
||||
}
|
||||
|
||||
@@ -94,11 +94,11 @@ auto Part2(Grid const& grid, Coord base, std::size_t n) {
|
||||
std::map<Rational<std::int64_t>, std::vector<Coord>> targets;
|
||||
|
||||
// arrange all the other asteroids by their angle relative to the base
|
||||
grid.each([&](Coord c, char v){
|
||||
for (auto [c, v] : grid){
|
||||
if (c != base && v == '#') {
|
||||
targets[angle(c-base)].push_back(c);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Sort to vectors per angle such that the nearest asteroids are at
|
||||
// the end of the list
|
||||
|
@@ -66,14 +66,14 @@ auto ComputePath(Grid const& grid)
|
||||
{
|
||||
// Determine starting location and direction
|
||||
Coord start{}, step{};
|
||||
grid.each([&](Coord c, char v) {
|
||||
for (auto [c, v] : grid) {
|
||||
switch (v) {
|
||||
case '^': start = c; step = { 0, -1}; break;
|
||||
case '>': start = c; step = { 0, 1}; break;
|
||||
case '<': start = c; step = {-1, 0}; break;
|
||||
case 'v': start = c; step = { 0, 1}; break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
auto is_path = [&](Coord c) {
|
||||
return grid.contains(c) && grid[c] == '#';
|
||||
|
@@ -41,11 +41,11 @@ auto SetKey(Doors& doors, char key) {
|
||||
|
||||
auto FindFeatures(Grid const& grid) -> Features {
|
||||
Features features;
|
||||
grid.each([&](Coord c, char v) {
|
||||
for (auto [c, v] : grid) {
|
||||
if ('#' != v && '.' != v) {
|
||||
features[v] = c;
|
||||
}
|
||||
});
|
||||
}
|
||||
return features;
|
||||
}
|
||||
|
||||
|
@@ -27,9 +27,9 @@ auto FindBugs(Grid const& grid) -> std::vector<Coord> {
|
||||
std::int64_t w = grid.Cols();
|
||||
std::int64_t h = grid.Rows();
|
||||
|
||||
grid.each([&](Coord c, char v) {
|
||||
for (auto [c, v] : grid) {
|
||||
if (v == '#') result.push_back(c);
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user