From fef2d224ed2e53e5b33e030f06491b43047bbbe2 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Tue, 10 Dec 2024 19:48:00 +0100 Subject: aoc 2024, day 10, cleanup --- 2024/src/day10.cpp | 133 ++++++++++++++++++++++++++--------------------------- 1 file changed, 64 insertions(+), 69 deletions(-) (limited to '2024') diff --git a/2024/src/day10.cpp b/2024/src/day10.cpp index 58b797f..73e4f92 100644 --- a/2024/src/day10.cpp +++ b/2024/src/day10.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -33,42 +34,8 @@ neighbors(pos_type pos) return { { x - 1, y }, { x, y - 1 }, { x + 1, y }, { x, y + 1 } }; } -size_t -bfs(const map& data, pos_type start_pos) -{ - size_t peaks = 0; - - set seen; - queue queue; - - queue.emplace(start_pos); - seen.emplace(start_pos); - - while ( !queue.empty() ) { - auto pos = queue.front(); - queue.pop(); - - if ( data.at(pos) == 9 ) { - ++peaks; - continue; - } - - for ( const auto& neighbor: neighbors(pos) ) { - if ( !data.contains(neighbor) || - seen.contains(neighbor) || - data.at(neighbor) != data.at(pos) + 1 ) { - continue; - } - - queue.emplace(neighbor); - seen.insert(neighbor); - } - } - return peaks; -} - -void -part1(const map& data) +vector +get_starts(const map& data) { vector starts; for ( const auto& [pos, value]: data ) { @@ -76,59 +43,87 @@ part1(const map& data) starts.push_back(pos); } } - - size_t sum = 0; - for ( const auto& start: starts ) { - sum += bfs(data, start); - } - cout << sum << endl; + return starts; } +template size_t -bfs2(const map& data, pos_type start_pos) +solve(const map& data, BFS bfs) { - size_t paths = 0; + auto starts = get_starts(data); + return accumulate(starts.begin(), starts.end(), 0U, [&](auto init, auto start) { return init + bfs(data, start); }); +} - queue queue; +void +part1(const map& data) +{ + auto bfs = [](const map& data, pos_type start_pos) -> size_t { + size_t score = 0; - queue.push(start_pos); + set seen; + queue queue; - while ( !queue.empty() ) { - auto pos = queue.front(); - queue.pop(); + queue.emplace(start_pos); + seen.emplace(start_pos); - if ( data.at(pos) == 9 ) { - ++paths; - continue; - } + while ( !queue.empty() ) { + auto pos = queue.front(); + queue.pop(); - for ( const auto& neighbor: neighbors(pos) ) { - if ( !data.contains(neighbor) || - data.at(neighbor) != data.at(pos) + 1 ) { + if ( data.at(pos) == 9 ) { + ++score; continue; } - queue.emplace(neighbor); + for ( const auto& neighbor: neighbors(pos) ) { + if ( !data.contains(neighbor) || + seen.contains(neighbor) || + data.at(neighbor) != data.at(pos) + 1 ) { + continue; + } + + queue.emplace(neighbor); + seen.insert(neighbor); + } } - } - return paths; + return score; + }; + + cout << solve(data, bfs) << endl; } void part2(const map& data) { - vector starts; - for ( const auto& [pos, value]: data ) { - if ( value == 0 ) { - starts.push_back(pos); + auto bfs = [](const map& data, pos_type start_pos) -> size_t { + size_t score = 0; + + queue queue; + + queue.push(start_pos); + + while ( !queue.empty() ) { + auto pos = queue.front(); + queue.pop(); + + if ( data.at(pos) == 9 ) { + ++score; + continue; + } + + for ( const auto& neighbor: neighbors(pos) ) { + if ( !data.contains(neighbor) || + data.at(neighbor) != data.at(pos) + 1 ) { + continue; + } + + queue.emplace(neighbor); + } } - } + return score; + }; - size_t sum = 0; - for ( const auto& start: starts ) { - sum += bfs2(data, start); - } - cout << sum << endl; + cout << solve(data, bfs) << endl; } int -- cgit v1.3