aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2024-01-18 17:35:47 +0100
committerThomas Schmucker <ts@its1.de>2024-01-18 17:35:47 +0100
commit6cd126ef14920341524bf7f54100443c8e2345c1 (patch)
tree5085fc10b976aa1e359eb966a70d3fce5fd52abf
parentb68f250e3a88113362eeb628db5e1f9c3b7bfc5b (diff)
downloadadvent-of-code-6cd126ef14920341524bf7f54100443c8e2345c1.tar.gz
advent-of-code-6cd126ef14920341524bf7f54100443c8e2345c1.tar.bz2
advent-of-code-6cd126ef14920341524bf7f54100443c8e2345c1.zip
much simpler count function
-rw-r--r--2023/src/day21.cpp33
1 files 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 @@
1#include <cstddef>
2#include <fstream> 1#include <fstream>
3#include <iostream> 2#include <iostream>
4#include <queue>
5#include <set> 3#include <set>
6#include <string> 4#include <string>
7#include <vector> 5#include <vector>
@@ -56,35 +54,20 @@ find_neighbours(position pos, const vector<string>& lines)
56} 54}
57 55
58size_t 56size_t
59count(vector<string> lines, position start, size_t rounds) 57count(const vector<string>& lines, position start, size_t rounds)
60{ 58{
61 queue<position> positions; 59 set<position> positions;
62 positions.emplace(start); 60 positions.emplace(start);
63 61
64 size_t sum = 0;
65 for ( size_t round = 0; round != rounds; ++round ) { 62 for ( size_t round = 0; round != rounds; ++round ) {
66 set<position> next_positions; 63 set<position> new_positions;
67 64 for ( const auto& position: positions ) {
68 sum = 0; 65 const auto neighbours = find_neighbours(position, lines);
69 while ( !positions.empty() ) { 66 new_positions.insert(neighbours.begin(), neighbours.end());
70 const auto [curr_row, curr_col] = positions.front();
71 lines[curr_row][curr_col] = '.';
72
73 const auto neighbours = find_neighbours(positions.front(), lines);
74 positions.pop();
75
76 for ( const auto& [row, col]: neighbours ) {
77 lines[row][col] = 'O';
78 next_positions.emplace(row, col);
79 ++sum;
80 }
81 }
82
83 for ( const auto& position: next_positions ) {
84 positions.emplace(position);
85 } 67 }
68 positions = std::move(new_positions);
86 } 69 }
87 return sum; 70 return positions.size();
88} 71}
89 72
90void 73void