This commit is contained in:
Eric Mertens 2022-11-27 11:43:10 -08:00
parent 07567e4c8a
commit c8470bb8dc
5 changed files with 19 additions and 34 deletions

View File

@ -1,5 +1,4 @@
#include <cstdint> #include <cstdint>
#include <deque>
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <numeric> #include <numeric>

View File

@ -1,5 +1,5 @@
#include <algorithm> #include <algorithm>
#include <deque> #include <queue>
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <tuple> #include <tuple>
@ -70,9 +70,11 @@ auto Compute(std::map<Coord, ValueType> world) -> std::pair<int, int> {
int part1 = -1; int part1 = -1;
int part2 = -1; int part2 = -1;
for (std::deque<std::pair<int, Coord>> todo {{0,start}}; for (
std::queue<std::pair<int, Coord>> todo
{decltype(todo)::container_type{{0,start}}};
!todo.empty(); !todo.empty();
todo.pop_front()) todo.pop())
{ {
auto [steps, here] = todo.front(); auto [steps, here] = todo.front();
if (Coord{0,0} == here) part1 = steps; if (Coord{0,0} == here) part1 = steps;
@ -81,7 +83,7 @@ auto Compute(std::map<Coord, ValueType> world) -> std::pair<int, int> {
for (auto fn : moves) { for (auto fn : moves) {
auto next = fn(here); auto next = fn(here);
if (world.erase(next)) { if (world.erase(next)) {
todo.emplace_back(steps+1, next); todo.emplace(steps+1, next);
} }
} }
} }

View File

@ -10,7 +10,7 @@
#include <string> #include <string>
#include <tuple> #include <tuple>
#include <bitset> #include <bitset>
#include <deque> #include <queue>
#include <cctype> #include <cctype>
#include <queue> #include <queue>
#include <map> #include <map>
@ -56,11 +56,12 @@ auto FindDistancesFrom(
) { ) {
std::map<char, std::pair<std::int64_t, Doors>> result; std::map<char, std::pair<std::int64_t, Doors>> result;
std::deque<std::tuple<std::int64_t, Coord, Doors>> todo {{0, start, {}}}; std::queue<std::tuple<std::int64_t, Coord, Doors>> todo;
todo.push({0, start, {}});
std::set<Coord> seen; std::set<Coord> seen;
for (; !todo.empty(); todo.pop_front()) { for (; !todo.empty(); todo.pop()) {
auto [steps, here, doors] = todo.front(); auto [steps, here, doors] = todo.front();
// Don't visit the same coordinate twice // Don't visit the same coordinate twice
@ -82,7 +83,7 @@ auto FindDistancesFrom(
// Visit all neighbors // Visit all neighbors
for (auto const fn : directions) { for (auto const fn : directions) {
todo.emplace_back(steps+1, fn(here), doors); todo.emplace(steps+1, fn(here), doors);
} }
} }

View File

@ -10,7 +10,7 @@
#include <string> #include <string>
#include <tuple> #include <tuple>
#include <bitset> #include <bitset>
#include <deque> #include <queue>
#include <cctype> #include <cctype>
#include <queue> #include <queue>
#include <map> #include <map>
@ -64,12 +64,12 @@ auto FindDistancesFrom(
Coord const start Coord const start
) { ) {
std::vector<std::pair<Name, std::int64_t>> result; std::vector<std::pair<Name, std::int64_t>> result;
std::deque<std::pair<std::int64_t, Coord>> todo {{0, start}};
std::set<Coord> seen; std::set<Coord> seen;
for (
for (; !todo.empty(); todo.pop_front()) { std::queue<std::pair<std::int64_t, Coord>> todo
{decltype(todo)::container_type{{{0, start}}}};
!todo.empty();
todo.pop()) {
auto const [steps, here] = todo.front(); auto const [steps, here] = todo.front();
// Don't visit the same coordinate twice // Don't visit the same coordinate twice
@ -86,7 +86,7 @@ auto FindDistancesFrom(
// Visit all neighbors // Visit all neighbors
for (auto const fn : directions) { for (auto const fn : directions) {
todo.emplace_back(steps+1, fn(here)); todo.emplace(steps+1, fn(here));
} }
} }

View File

@ -1,17 +0,0 @@
{
"version": 2,
"configurePresets": [
{
"name": "preset",
"displayName": "Configure preset using toolchain file",
"description": "Sets Ninja generator, build and install directory",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_TOOLCHAIN_FILE": "",
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}"
}
}
]
}