mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-27 05:40:32 +01:00
Implement day 15 part 2.
I don't know why my intcode was broken for so long.
This commit is contained in:
@@ -94,32 +94,35 @@ namespace {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void aoc2019::day15_part1(std::istream &input, std::ostream &output) {
|
template<class Callback>
|
||||||
const auto map = read_map(input);
|
int bfs(const std::unordered_map<point_t, Tile> &map, point_t starting_point, Callback callback) {
|
||||||
|
std::deque<std::pair<point_t, int>> todo{{starting_point, 0}};
|
||||||
std::deque<std::pair<point_t, int>> todo{{{0, 0}, 0}};
|
|
||||||
std::unordered_set<point_t> visited{{0, 0}};
|
std::unordered_set<point_t> visited{{0, 0}};
|
||||||
|
|
||||||
|
int max_dist = 0;
|
||||||
|
|
||||||
while (!todo.empty()) {
|
while (!todo.empty()) {
|
||||||
auto[cur, dist] = todo.front();
|
auto[cur, dist] = todo.front();
|
||||||
todo.pop_front();
|
todo.pop_front();
|
||||||
|
|
||||||
|
max_dist = std::max(max_dist, dist);
|
||||||
|
|
||||||
for (auto &dir : DIRECTIONS) {
|
for (auto &dir : DIRECTIONS) {
|
||||||
auto new_pos = cur + dir.first;
|
auto new_pos = cur + dir.first;
|
||||||
if (!visited.count(new_pos)) {
|
if (!visited.count(new_pos)) {
|
||||||
visited.insert(new_pos);
|
visited.insert(new_pos);
|
||||||
|
|
||||||
|
if (callback(map.at(new_pos))) {
|
||||||
|
return dist + 1;
|
||||||
|
}
|
||||||
|
|
||||||
switch (map.at(new_pos)) {
|
switch (map.at(new_pos)) {
|
||||||
|
case Tile::Oxygen:
|
||||||
case Tile::Empty:
|
case Tile::Empty:
|
||||||
todo.emplace_back(new_pos, dist + 1);
|
todo.emplace_back(new_pos, dist + 1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Tile::Oxygen:
|
|
||||||
output << dist + 1 << std::endl;
|
|
||||||
return;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -127,9 +130,24 @@ void aoc2019::day15_part1(std::istream &input, std::ostream &output) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw std::domain_error("No route to oxygen.");
|
return max_dist;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void aoc2019::day15_part1(std::istream &input, std::ostream &output) {
|
||||||
|
const auto map = read_map(input);
|
||||||
|
|
||||||
|
auto dist = bfs(map, {0, 0}, [](Tile x) { return x == Tile::Oxygen; });
|
||||||
|
|
||||||
|
output << dist << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void aoc2019::day15_part2(std::istream &input, std::ostream &output) {
|
void aoc2019::day15_part2(std::istream &input, std::ostream &output) {
|
||||||
output << "Not implemented\n";
|
const auto map = read_map(input);
|
||||||
|
|
||||||
|
auto starting_point = std::find_if(map.begin(), map.end(), [](auto x) { return x.second == Tile::Oxygen; })->first;
|
||||||
|
|
||||||
|
auto dist = bfs(map, starting_point, [](Tile x) { return false; });
|
||||||
|
|
||||||
|
output << dist << std::endl;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user