From 6cd126ef14920341524bf7f54100443c8e2345c1 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Thu, 18 Jan 2024 17:35:47 +0100 Subject: much simpler count function --- 2023/src/day21.cpp | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/2023/src/day21.cpp b/2023/src/day21.cpp index c633155..21a45f7 100644 --- a/2023/src/day21.cpp +++ b/2023/src/day21.cpp @@ -1,7 +1,5 @@ -#include #include #include -#include #include #include #include @@ -56,35 +54,20 @@ find_neighbours(position pos, const vector& lines) } size_t -count(vector lines, position start, size_t rounds) +count(const vector& lines, position start, size_t rounds) { - queue positions; + set positions; positions.emplace(start); - size_t sum = 0; for ( size_t round = 0; round != rounds; ++round ) { - set next_positions; - - sum = 0; - while ( !positions.empty() ) { - const auto [curr_row, curr_col] = positions.front(); - lines[curr_row][curr_col] = '.'; - - const auto neighbours = find_neighbours(positions.front(), lines); - positions.pop(); - - for ( const auto& [row, col]: neighbours ) { - lines[row][col] = 'O'; - next_positions.emplace(row, col); - ++sum; - } - } - - for ( const auto& position: next_positions ) { - positions.emplace(position); + set new_positions; + for ( const auto& position: positions ) { + const auto neighbours = find_neighbours(position, lines); + new_positions.insert(neighbours.begin(), neighbours.end()); } + positions = std::move(new_positions); } - return sum; + return positions.size(); } void -- cgit v1.3