From d6ae60aed827c742de956194223b4640b5403b72 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Thu, 28 Dec 2023 11:16:26 +0100 Subject: cleanup code --- src/day23.cpp | 59 ++++++++--------------------------------------------------- 1 file changed, 8 insertions(+), 51 deletions(-) (limited to 'src/day23.cpp') diff --git a/src/day23.cpp b/src/day23.cpp index 207a62c..d8c20d3 100644 --- a/src/day23.cpp +++ b/src/day23.cpp @@ -110,58 +110,14 @@ get_neighbours2(const vector& maze, position_t possition) } void -part1(const vector& maze) -{ - const position_t start = { 0, maze[0].find('.') }; - const position_t end = { maze.size() - 1, maze[maze.size() - 1].find('.') }; - - queue>> queue; - - queue.emplace(start, set{}); - - size_t longest = 0; - while ( !queue.empty() ) { - auto [current_position, seen] = queue.front(); - queue.pop(); - - while ( current_position != end ) { - seen.emplace(current_position); - - const auto neighbours = get_neighbours(maze, current_position); - - vector next_positions; - - // { neighbours \ seen } - set_difference(neighbours.begin(), neighbours.end(), - seen.begin(), seen.end(), - back_inserter(next_positions)); - - if ( next_positions.empty() ) { - break; - } - - current_position = next_positions[0]; - - for ( size_t idx = 1; idx != next_positions.size(); ++idx ) { - queue.emplace(next_positions[idx], seen); - } - } - if ( current_position == end ) { - longest = max(longest, seen.size()); - } - } - cout << longest << endl; -} - -void -part2(const vector& maze) +solve(const vector& maze, function(const vector&, position_t)> neighbours) { const position_t start = { 0, maze[0].find('.') }; const position_t end = { maze.size() - 1, maze[maze.size() - 1].find('.') }; vector> visited(maze.size(), vector(maze[0].size())); - function findmax = [&](position_t position, long current) -> long { + function find_longest_path = [&](position_t position, long current) -> long { const auto [row, col] = position; if ( visited[row][col] ) { @@ -173,16 +129,17 @@ part2(const vector& maze) } long value = 0; + visited[row][col] = true; - for ( const auto& neighbour: get_neighbours2(maze, position) ) { - value = max(value, findmax(neighbour, current + 1)); + for ( const auto& neighbour: neighbours(maze, position) ) { + value = max(value, find_longest_path(neighbour, current + 1)); } visited[row][col] = false; return value; }; - auto value = findmax(start, 0); + auto value = find_longest_path(start, 0); cout << value << endl; } @@ -190,6 +147,6 @@ int main() { const auto maze = read_file("data/day23.txt"); - part1(maze); - part2(maze); + solve(maze, get_neighbours); + solve(maze, get_neighbours2); } -- cgit v1.3