Skip to content

Commit

Permalink
Update previous code to use new Grid helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
yut23 committed Jan 4, 2025
1 parent cfff7e2 commit 3ab365b
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 205 deletions.
20 changes: 7 additions & 13 deletions 2023/src/day14.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define DAY14_HPP_A7H2MCKZ

#include "ds/grid.hpp" // for Grid
#include "lib.hpp" // for Pos
#include <cstddef> // for size_t
#include <iostream> // for istream
#include <string> // for string, getline
Expand Down Expand Up @@ -115,13 +116,11 @@ void Platform::tilt() {

int Platform::calculate_load() const {
int load = 0;
for (int y = 0; y < rocks.height; ++y) {
for (int x = 0; x < rocks.width; ++x) {
if (rocks.at_unchecked(x, y) == Rock::round) {
load += rocks.width - x;
}
rocks.for_each([&load, width = rocks.width](Rock rock, const Pos &pos) {
if (rock == Rock::round) {
load += width - pos.x;
}
}
});
return load;
}

Expand All @@ -136,13 +135,8 @@ std::vector<bool> Platform::round_rocks() const {
}

std::ostream &operator<<(std::ostream &os, const Platform &platform) {
for (const auto &row : platform.rocks) {
for (Rock rock : row) {
os << static_cast<char>(rock);
}
os << "\n";
}
return os;
return platform.rocks.custom_print(
os, [&os](Rock rock) { os << static_cast<char>(rock); });
}

Platform read_platform(std::istream &is) {
Expand Down
25 changes: 8 additions & 17 deletions 2023/src/day16.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,13 @@ LaserGrid LaserGrid::read(std::istream &is) {
}

void LaserGrid::print_energized(std::ostream &os) const {
for (const auto &row : *this) {
for (const auto &tile : row) {
os << (tile.energized ? '#' : '.');
}
os << '\n';
}
custom_print(
os, [&os](const Tile &tile) { os << (tile.energized ? '#' : '.'); });
}

std::ostream &operator<<(std::ostream &os, const LaserGrid &laser_grid) {
for (const auto &row : laser_grid) {
for (const auto &tile : row) {
os << static_cast<char>(tile.type);
}
os << '\n';
}
return os;
return laser_grid.custom_print(
os, [&os](const Tile &tile) { os << static_cast<char>(tile.type); });
}

///
Expand Down Expand Up @@ -177,11 +168,11 @@ struct GraphHelper {
std::vector<std::vector<int>> component_successors;
std::unordered_map<Key, int> source_components;

void process_neighbors(const Key &key, auto &&handler) const {
void process_neighbors(const Key &key, auto &&process) const {
for (AbsDirection dir : grid[key.pos].get_out_dir(key.dir)) {
Pos new_pos = key.pos + Delta(dir, true);
if (grid.in_bounds(new_pos)) {
handler({new_pos, dir});
process({new_pos, dir});
}
}
}
Expand Down Expand Up @@ -255,8 +246,8 @@ int GraphHelper::count_energized(const GraphHelper::Key &source) const {
constexpr bool use_seen = false;
aoc::graph::dfs<use_seen>(
source_components.at(source),
[this](int id, auto &&handler) {
process_component_neighbors(id, handler);
[this](int id, auto &&process) {
process_component_neighbors(id, process);
},
/*is_target*/ {}, visit_with_parent);

Expand Down
11 changes: 4 additions & 7 deletions 2023/src/day17.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,8 @@ void CityMap::print(std::ostream &os, const std::vector<Key> &path) const {
path_lookup[key.pos] = &key;
}

Pos pos;
for (pos.y = 0; pos.y < block_costs.height; ++pos.y) {
for (pos.x = 0; pos.x < block_costs.width; ++pos.x) {
block_costs.custom_print(
os, [&os, &path_lookup](unsigned int cost, const Pos &pos) {
auto it = path_lookup.find(pos);
if (it != path_lookup.end()) {
switch (it->second->orient) {
Expand All @@ -189,11 +188,9 @@ void CityMap::print(std::ostream &os, const std::vector<Key> &path) const {
break;
}
} else {
os << as_number{block_costs[pos]};
os << aoc::as_number{cost};
}
}
os << '\n';
}
});
}

CityMap CityMap::read(std::istream &is) {
Expand Down
29 changes: 12 additions & 17 deletions 2023/src/day21.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@

#include "ds/grid.hpp" // for Grid
#include "graph_traversal.hpp" // for bfs
#include "lib.hpp" // for AbsDirection, Pos, Delta, DIRECTIONS, DEBUG
#include <array> // for array
#include <cassert> // for assert
#include <compare> // for strong_ordering
#include <cstdlib> // for abs, size_t
#include <initializer_list> // for initializer_list
#include <iostream> // for istream, noskipws, cerr
#include <limits> // for numeric_limits
#include <utility> // for move
#include <vector> // for vector
// IWYU pragma: no_include <algorithm> // for copy
#include "lib.hpp" // for Pos, DEBUG
#include <array> // for array
#include <cassert> // for assert
#include <compare> // for strong_ordering
#include <cstdlib> // for abs, size_t
#include <iostream> // for istream, noskipws, cerr
#include <limits> // for numeric_limits
#include <vector> // for vector

namespace aoc::day21 {

Expand All @@ -43,13 +40,11 @@ struct Garden {

const auto process_neighbors = [this, &distances](const Key &key,
auto &&process) {
for (const AbsDirection &dir : aoc::DIRECTIONS) {
Pos pos = key + Delta(dir, true);
if (stones.in_bounds(pos) && stones[pos] &&
distances[pos] == std::numeric_limits<int>::max()) {
process(std::move(pos));
stones.manhattan_kernel(key, [&](bool open, const Pos &pos) {
if (open && distances[pos] == std::numeric_limits<int>::max()) {
process(pos);
}
}
});
};
const auto visit = [&source, &distances](const Key &key,
int distance) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion 2023/src/day23.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ bool TrailMap::get_grid_neighbors(const Pos &pos, const Pos &prev_pos,
dir != allowed_directions.at(new_terrain)) {
continue;
}
neighbors.push_back(new_pos);
neighbors.push_back(std::move(new_pos));
}
assert(neighbors.size() <= 2);
break;
Expand Down
2 changes: 1 addition & 1 deletion 2024/src/day04.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace aoc::day04 {

using aoc::ds::Grid, aoc::Pos, aoc::Delta;
using aoc::ds::Grid;

constexpr std::initializer_list<Delta> ADJ_DELTAS = {
// clang-format off
Expand Down
67 changes: 30 additions & 37 deletions 2024/src/day06.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,46 +191,39 @@ int GuardSim::count_visited() const {
}

std::ostream &operator<<(std::ostream &os, const GuardSim &sim) {
Pos pos(0, 0);
for (pos.y = 0; const auto &row : sim) {
for (pos.x = 0; const auto &tile : row) {
char ch = '.';
if (tile.is_visited()) {
using enum AbsDirection;
bool vert = tile.is_visited(north) || tile.is_visited(south);
bool horz = tile.is_visited(east) || tile.is_visited(west);
if (vert && horz) {
ch = '+';
} else {
ch = vert ? '|' : '-';
}
return sim.custom_print(os, [&os, &sim](auto &tile, const Pos &pos) {
char ch = '.';
if (tile.is_visited()) {
using enum AbsDirection;
bool vert = tile.is_visited(north) || tile.is_visited(south);
bool horz = tile.is_visited(east) || tile.is_visited(west);
if (vert && horz) {
ch = '+';
} else {
ch = vert ? '|' : '-';
}
if (tile.blocked) {
ch = tile.blocked == 2 ? 'O' : '#';
}
if (sim.guard_pos == pos) {
switch (sim.guard_dir) {
case AbsDirection::north:
ch = '^';
break;
case AbsDirection::east:
ch = '>';
break;
case AbsDirection::south:
ch = 'v';
break;
case AbsDirection::west:
ch = '<';
break;
}
}
if (tile.blocked) {
ch = tile.blocked == 2 ? 'O' : '#';
}
if (sim.guard_pos == pos) {
switch (sim.guard_dir) {
case AbsDirection::north:
ch = '^';
break;
case AbsDirection::east:
ch = '>';
break;
case AbsDirection::south:
ch = 'v';
break;
case AbsDirection::west:
ch = '<';
break;
}
os << ch;
++pos.x;
}
os << "\n";
++pos.y;
}
return os;
os << ch;
});
}

} // namespace aoc::day06
Expand Down
48 changes: 20 additions & 28 deletions 2024/src/day10.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include "ds/grid.hpp" // for Grid
#include "graph_traversal.hpp" // for bfs
#include "lib.hpp" // for Pos, as_number, DEBUG, DIRECTIONS, Delta
#include "lib.hpp" // for Pos, as_number, DEBUG

#include <algorithm> // for transform
#include <cstddef> // for size_t
Expand All @@ -20,7 +20,6 @@
#include <string> // for string, getline
#include <utility> // for move
#include <vector> // for vector
// IWYU pragma: no_include <initializer_list> // for aoc::DIRECTIONS

namespace aoc::day10 {

Expand Down Expand Up @@ -68,17 +67,13 @@ IslandMap::IslandMap(std::vector<std::vector<height_t>> &&height_map)
}

std::ostream &operator<<(std::ostream &os, const IslandMap &island) {
for (const auto &row : island) {
for (const auto tile : row) {
if (tile == IMPASSABLE) {
os << '.';
} else {
os << aoc::as_number(tile);
}
return island.custom_print(os, [&os](const auto tile) {
if (tile == IMPASSABLE) {
os << '.';
} else {
os << aoc::as_number(tile);
}
os << '\n';
}
return os;
});
}

/**
Expand Down Expand Up @@ -114,13 +109,13 @@ void IslandMap::process_neighbors(const Pos &pos, auto &&process) const {
if (curr_height == 0) {
return;
}
// loop over cardinal directions
for (const auto &dir : DIRECTIONS) {
Pos neighbor = pos + Delta(dir, true);
if (in_bounds(neighbor) && (*this)[neighbor] == curr_height - 1) {
process(neighbor);
}
}
// loop over neighboring positions
manhattan_kernel(
pos, [&process, curr_height](height_t new_height, const Pos &neighbor) {
if (new_height == curr_height - 1) {
process(neighbor);
}
});
}

template <aoc::Part part>
Expand All @@ -131,16 +126,13 @@ int IslandMap::trailhead_scores() const {
if constexpr (aoc::DEBUG) {
std::cerr << "map:\n" << *this << "\n";
std::cerr << "scores:\n";
for (const auto &row : scores) {
for (const auto score : row) {
if (score == 0) {
std::cerr << '.';
} else {
std::cerr << score;
}
scores.custom_print(std::cerr, [](const auto score) {
if (score == 0) {
std::cerr << '.';
} else {
std::cerr << score;
}
std::cerr << '\n';
}
});
}
int total_score = 0;
for (const Pos &pos : trailheads) {
Expand Down
7 changes: 3 additions & 4 deletions 2024/src/day12.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Garden {
return '.';
}

void process_plot(const Pos &pos, char plant_type);
void process_plot(char plant_type, const Pos &pos);

public:
static Garden read(std::istream &is);
Expand All @@ -65,12 +65,11 @@ class Garden {

Garden Garden::read(std::istream &is) {
Garden garden{aoc::ds::Grid<char>{aoc::read_lines(is)}};
garden.plots.for_each_with_pos(
std::bind_front(&Garden::process_plot, &garden));
garden.plots.for_each(std::bind_front(&Garden::process_plot, &garden));
return garden;
}

void Garden::process_plot(const Pos &pos, char plant_type) {
void Garden::process_plot(char plant_type, const Pos &pos) {
// flood-fill plots with matching plant types
if (region_indices[pos] != -1) {
// this plot is already part of a region
Expand Down
20 changes: 8 additions & 12 deletions 2024/src/day14.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,15 @@ int Robots::largest_clump() const {
}

std::ostream &operator<<(std::ostream &os, const Robots &robots) {
for (const auto &row : robots.robot_counts) {
for (const auto &count : row) {
if (count == 0) {
os << ' ';
} else if (count < 10) {
os << count;
} else {
os << '*';
}
return robots.robot_counts.custom_print(os, [&os](auto count) {
if (count == 0) {
os << ' ';
} else if (count < 10) {
os << count;
} else {
os << '*';
}
os << "\n";
}
return os;
});
}

} // namespace aoc::day14
Expand Down
Loading

0 comments on commit 3ab365b

Please sign in to comment.