Skip to content

Commit 70034ba

Browse files
committed
Day 10: Use transform_reduce instead of for-loop
1 parent 6088aaa commit 70034ba

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ list(LENGTH TARGETS NUM_TARGETS)
3737
foreach(current_target IN LISTS TARGETS)
3838
add_executable(${current_target} ${current_target}/${current_target}.cpp)
3939
set_target_properties(${current_target} PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})
40-
target_include_directories(${current_target} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/aoclib/")
40+
target_include_directories(${current_target} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/")
4141

4242
target_compile_options(${current_target} PRIVATE ${WARNING_FLAGS_CXX} $<$<CONFIG:Debug>:${DBG_FLAGS_CXX}>)
4343
target_link_options(${current_target} PRIVATE ${WARNING_FLAGS_CXX} $<$<CONFIG:Debug>:${DBG_FLAGS_CXX}>)

aoclib/grid.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ class Grid
341341
}
342342

343343
public:
344+
typedef ElemType value_type;
345+
344346
Grid() = default;
345347

346348
Grid(const std::vector<RowType>& rows)

aoclib/vec.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace aocutil
99
template<typename T>
1010
struct Vec2
1111
{
12+
typedef T value_type;
1213
T x, y;
1314

1415
Vec2 operator+(const Vec2& v) const

day-10/day-10.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include <stack>
2-
#include "../aoclib/aocio.hpp"
3-
#include "../aoclib/grid.hpp"
4-
#include "../aoclib/vec.hpp"
2+
#include <numeric>
3+
#include "aoclib/aocio.hpp"
4+
#include "aoclib/grid.hpp"
5+
#include "aoclib/vec.hpp"
56

67
/*
78
Problem: https://adventofcode.com/2024/day/10
@@ -23,13 +24,14 @@ using Vec2 = aocutil::Vec2<int>;
2324
int part_one(const std::vector<std::string>& lines, bool part_two = false)
2425
{
2526
Grid<int> height_map;
26-
for (const auto& line : lines) {
27-
std::vector<int> row;
27+
28+
for (const auto& line : lines) {
29+
std::vector<decltype(height_map)::value_type> row;
2830
std::transform(line.cbegin(), line.cend(), std::back_inserter(row), [](char c) {
2931
return aocio::parse_digit(c).value();
3032
});
3133
height_map.push_row(row);
32-
}
34+
}
3335

3436
const auto get_adjacent = [&height_map = std::as_const(height_map)](const Vec2& pos) {
3537
const auto dirs = aocutil::all_dirs_vec2<int>();
@@ -42,21 +44,19 @@ int part_one(const std::vector<std::string>& lines, bool part_two = false)
4244
return neighbors;
4345
};
4446

45-
int result = 0;
46-
const std::vector<Vec2> trailheads = height_map.find_elem_positions_if([](int h) {return h == 0;});
47-
for (const auto& trailhead : trailheads) {
48-
int score = 0;
47+
const auto trailhead_score = [&height_map = std::as_const(height_map), &get_adjacent, part_two](const Vec2& trailhead) -> int {
48+
int score = 0;
4949
std::stack<Vec2> positions;
5050
positions.push(trailhead);
5151
Grid<bool> reached(height_map.width(), height_map.height(), false);
5252

5353
while (!positions.empty()) { // Depth-first search.
5454
const Vec2 pos = positions.top();
5555
positions.pop();
56-
const int current_height = height_map.get(pos);
56+
const auto current_height = height_map.get(pos);
5757
std::vector<Vec2> adjacent = get_adjacent(pos);
5858
for (const auto& adj_pos : adjacent) {
59-
int neighbor_height = height_map.get(adj_pos);
59+
const auto neighbor_height = height_map.get(adj_pos);
6060
if (neighbor_height == current_height + 1) {
6161
if (neighbor_height == 9 && !reached.get(adj_pos)) {
6262
reached.set(adj_pos, !part_two); // Only use the "reached" grid for Part 1 (i.e. count all paths for Part 2)
@@ -67,9 +67,11 @@ int part_one(const std::vector<std::string>& lines, bool part_two = false)
6767
}
6868
}
6969
}
70-
result += score;
71-
}
72-
return result;
70+
return score;
71+
};
72+
73+
const std::vector<Vec2> trailheads = height_map.find_elem_positions_if([](auto h) {return h == 0;});
74+
return std::transform_reduce(trailheads.cbegin(), trailheads.cend(), int{0}, std::plus{}, trailhead_score);
7375
}
7476

7577
int part_two(const std::vector<std::string>& lines)

0 commit comments

Comments
 (0)