This commit is contained in:
Eric Mertens 2022-11-11 08:27:18 -08:00
parent 4a8b31bd86
commit 346283bc8a
7 changed files with 22 additions and 28 deletions

View File

@ -17,7 +17,7 @@ auto Part1(std::string const& line) {
std::distance(it, line.end()) >= 150; std::distance(it, line.end()) >= 150;
std::advance(it, 150)) std::advance(it, 150))
{ {
auto zeros = std::count(it, it + 150, '0'); auto zeros = std::size_t(std::count(it, it + 150, '0'));
if (zeros < seen) { if (zeros < seen) {
seen = zeros; seen = zeros;
part1 part1

View File

@ -21,8 +21,8 @@ struct Particle {
auto Step(std::vector<Particle> & ps) { auto Step(std::vector<Particle> & ps) {
// apply gravity to update velocities // apply gravity to update velocities
auto n = ps.size(); auto n = ps.size();
for (int i = 0; i+1 < n; i++) { for (std::size_t i = 0; i+1 < n; i++) {
for (int j = i+1; j < n; j++) { for (std::size_t j = i+1; j < n; j++) {
auto v = (ps[i].pos < ps[j].pos) - (ps[i].pos > ps[j].pos); auto v = (ps[i].pos < ps[j].pos) - (ps[i].pos > ps[j].pos);
ps[i].vel += v; ps[i].vel += v;
ps[j].vel -= v; ps[j].vel -= v;

View File

@ -56,11 +56,11 @@ struct Map {
auto FindFeatures(Map const& map) -> Features { auto FindFeatures(Map const& map) -> Features {
Features features; Features features;
for (std::int64_t y = 0; y < map.rows.size(); y++) { for (std::size_t y = 0; y < map.rows.size(); y++) {
for (std::int64_t x = 0; x < map.rows[y].size(); x++) { for (std::size_t x = 0; x < map.rows[y].size(); x++) {
auto const c = map[{x,y}]; auto const c = map[{std::int64_t(x),std::int64_t(y)}];
if ('#' != c && '.' != c) { if ('#' != c && '.' != c) {
features[c] = {x,y}; features[c] = {std::int64_t(x),std::int64_t(y)};
} }
} }
} }
@ -69,8 +69,6 @@ auto FindFeatures(Map const& map) -> Features {
auto FindDistancesFrom( auto FindDistancesFrom(
Map const& map, Map const& map,
Features const& features,
Distances & distances,
char const start_letter, char const start_letter,
Coord const start Coord const start
) { ) {
@ -91,7 +89,7 @@ auto FindDistancesFrom(
// success, we've found a key, record the path // success, we've found a key, record the path
if (c != start_letter && std::islower(c)) { if (c != start_letter && std::islower(c)) {
distances[start_letter][c] = {steps, doors}; result[c] = {steps, doors};
continue; // don't walk beyond the key continue; // don't walk beyond the key
} }
@ -113,7 +111,7 @@ auto FindDistances(Map const& map, Features const& features) {
Distances distances; Distances distances;
for (auto const [start_letter, start_coord] : features) { for (auto const [start_letter, start_coord] : features) {
if (!std::isupper(start_letter)) { if (!std::isupper(start_letter)) {
FindDistancesFrom(map, features, distances, start_letter, start_coord); distances[start_letter] = FindDistancesFrom(map, start_letter, start_coord);
} }
} }
return distances; return distances;
@ -148,7 +146,7 @@ auto SolveMaze(
for (auto& location : locations) { for (auto& location : locations) {
auto const save = location; auto const save = location;
for (auto const [next, costneed] : distances.at(location)) { for (auto const& [next, costneed] : distances.at(location)) {
auto const [cost, need] = costneed; auto const [cost, need] = costneed;
if ((need & ~keys).none()) { // no missing keys if ((need & ~keys).none()) { // no missing keys
location = next; location = next;

View File

@ -75,7 +75,6 @@ auto FindPortals(Map const& map) -> Portals {
auto FindDistancesFrom( auto FindDistancesFrom(
Map const& map, Map const& map,
std::map<Coord, Name> const& names, std::map<Coord, Name> const& names,
Distances & distances,
std::string const start_name, std::string const start_name,
Coord const start Coord const start
) { ) {
@ -123,7 +122,7 @@ auto FindDistances(Map const& map, Portals const& portals) {
} }
for (auto const& [start_name, start_coord] : portals) { for (auto const& [start_name, start_coord] : portals) {
distances[start_name] = FindDistancesFrom(map, names, distances, start_name, start_coord); distances[start_name] = FindDistancesFrom(map, names, start_name, start_coord);
} }
return distances; return distances;
@ -144,12 +143,12 @@ auto SolveMaze(Distances const& distances, bool const recursive) -> std::int64_t
todo.emplace(0, 0, "-AA"); todo.emplace(0, 0, "-AA");
while(!todo.empty()) { while(!todo.empty()) {
auto const [steps, depth, name] = todo.top(); auto const& [steps, depth, name] = todo.top();
todo.pop(); todo.pop();
if (name == "-ZZ") { return steps; } if (name == "-ZZ") { return steps; }
if (seen.emplace(depth, name).second) { if (seen.emplace(depth, name).second) {
if (auto const it = distances.find(name); it != distances.end()) { if (auto const it = distances.find(name); it != distances.end()) {
for (auto const [next, cost] : it->second) { for (auto const& [next, cost] : it->second) {
if (next == "-ZZ") { if (next == "-ZZ") {
if (depth == 0) todo.emplace(steps + cost, depth, "-ZZ"); if (depth == 0) todo.emplace(steps + cost, depth, "-ZZ");
} else { } else {

View File

@ -21,7 +21,7 @@ using Machines = std::array<Machine, 50>;
auto BuildNetwork(Machine m) -> Machines { auto BuildNetwork(Machine m) -> Machines {
Machines machines; Machines machines;
auto& i = std::get<Input>(Step(m)).input; auto& i = std::get<Input>(Step(m)).input;
for (i = 0; i < machines.size(); i++) { for (i = 0; i < std::int64_t(machines.size()); i++) {
machines[i] = m; machines[i] = m;
} }
return machines; return machines;

View File

@ -22,8 +22,6 @@ using namespace aocpp;
namespace { namespace {
Coord(* const directions[4])(Coord) = {Up, Down, Left, Right};
struct Map { struct Map {
std::vector<std::string> rows; std::vector<std::string> rows;
@ -58,10 +56,10 @@ auto FindBugs(Map const& map) {
auto Neighbor1(Coord c) { auto Neighbor1(Coord c) {
std::vector<Coord> result; std::vector<Coord> result;
result.reserve(4); result.reserve(4);
if (c.x > 0) result.push_back({c.x-1,c.y}); if (c.x > 0) result.push_back(Left(c));
if (c.x < 4) result.push_back({c.x+1,c.y}); if (c.x < 4) result.push_back(Right(c));
if (c.y > 0) result.push_back({c.x,c.y-1}); if (c.y > 0) result.push_back(Up(c));
if (c.y < 4) result.push_back({c.x,c.y+1}); if (c.y < 4) result.push_back(Down(c));
return result; return result;
} }
@ -75,7 +73,7 @@ auto Neighbor2(std::pair<Coord, std::int64_t> cd) {
result.push_back({{4,yi},d+1}); result.push_back({{4,yi},d+1});
} }
} else if (c.x > 0) { } else if (c.x > 0) {
result.push_back({{c.x-1,c.y},d}); result.push_back({Left(c),d});
} else { } else {
result.push_back({{1,2},d-1}); result.push_back({{1,2},d-1});
} }
@ -85,7 +83,7 @@ auto Neighbor2(std::pair<Coord, std::int64_t> cd) {
result.push_back({{0,yi},d+1}); result.push_back({{0,yi},d+1});
} }
} else if (c.x < 4) { } else if (c.x < 4) {
result.push_back({{c.x+1,c.y},d}); result.push_back({Right(c),d});
} else { } else {
result.push_back({{3,2},d-1}); result.push_back({{3,2},d-1});
} }
@ -95,7 +93,7 @@ auto Neighbor2(std::pair<Coord, std::int64_t> cd) {
result.push_back({{xi,4},d+1}); result.push_back({{xi,4},d+1});
} }
} else if (c.y > 0) { } else if (c.y > 0) {
result.push_back({{c.x,c.y-1},d}); result.push_back({Up(c),d});
} else { } else {
result.push_back({{2,1},d-1}); result.push_back({{2,1},d-1});
} }
@ -105,7 +103,7 @@ auto Neighbor2(std::pair<Coord, std::int64_t> cd) {
result.push_back({{xi,0},d+1}); result.push_back({{xi,0},d+1});
} }
} else if (c.y < 4) { } else if (c.y < 4) {
result.push_back({{c.x,c.y+1},d}); result.push_back({Down(c),d});
} else { } else {
result.push_back({{2,3},d-1}); result.push_back({{2,3},d-1});
} }

View File

@ -5,7 +5,6 @@
namespace intcode { namespace intcode {
auto ParseStream(std::istream &in) -> std::vector<ValueType> { auto ParseStream(std::istream &in) -> std::vector<ValueType> {
ValueType x;
std::string str; std::string str;
std::vector<ValueType> result; std::vector<ValueType> result;
while (std::getline(in, str, ',')) { while (std::getline(in, str, ',')) {