This commit is contained in:
Eric Mertens 2022-11-11 20:00:43 -08:00
parent 34a8e0d611
commit 29146ec1ee

View File

@ -33,7 +33,7 @@ struct Map {
} }
}; };
auto FindBugs(Map const& map) { auto FindBugs(Map const& map) -> std::vector<Coord> {
std::vector<Coord> result; std::vector<Coord> result;
std::int64_t w = map.rows[0].size(); std::int64_t w = map.rows[0].size();
@ -82,12 +82,12 @@ struct Neighbor2 {
} }
}; };
template<typename F, typename C> template<std::totally_ordered C, std::invocable<C, void(C const&)> F>
auto Step(std::vector<C> const& bugs, F with_neighbors = F{}) auto Step(std::vector<C> const& bugs, F with_neighbors = F{})
{ {
std::map<C, int> adjacent; std::map<C, int> adjacent;
for (auto const& x : bugs) { for (auto const& x : bugs) {
with_neighbors(x, [&adjacent](C n) -> void { adjacent[n]++; }); with_neighbors(x, [&adjacent](C const& n) -> void { adjacent[n]++; });
} }
std::vector<C> result; std::vector<C> result;
@ -120,18 +120,18 @@ auto Part1(std::vector<Coord> const& bugs) -> std::uint32_t {
std::set<std::uint32_t> seen; std::set<std::uint32_t> seen;
std::uint32_t bio; std::uint32_t bio;
while (seen.insert(bio = Bio(cursor)).second) { while (seen.insert(bio = Bio(cursor)).second) {
cursor = Step<Neighbor1>(cursor); cursor = Step<Coord, Neighbor1>(cursor);
} }
return bio; return bio;
} }
auto Part2(std::vector<Coord> const& bugs) { auto Part2(std::vector<Coord> const& bugs) {
std::vector<std::pair<Coord,std::int64_t>> bugs2; std::vector<Neighbor2::C3> bugs2;
for (auto const& [x,y] : bugs) { for (auto const& [x,y] : bugs) {
bugs2.push_back({{x-2,y-2},0}); bugs2.push_back({{x-2,y-2},0});
} }
for (int i = 0; i < 200; i++) { for (int i = 0; i < 200; i++) {
bugs2 = Step<Neighbor2>(bugs2); bugs2 = Step<Neighbor2::C3, Neighbor2>(bugs2);
} }
return bugs2.size(); return bugs2.size();
} }