diff options
Diffstat (limited to 'src/day23.cpp')
| -rw-r--r-- | src/day23.cpp | 59 |
1 files changed, 8 insertions, 51 deletions
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<string>& maze, position_t possition) | |||
| 110 | } | 110 | } |
| 111 | 111 | ||
| 112 | void | 112 | void |
| 113 | part1(const vector<string>& maze) | 113 | solve(const vector<string>& maze, function<set<position_t>(const vector<string>&, position_t)> neighbours) |
| 114 | { | ||
| 115 | const position_t start = { 0, maze[0].find('.') }; | ||
| 116 | const position_t end = { maze.size() - 1, maze[maze.size() - 1].find('.') }; | ||
| 117 | |||
| 118 | queue<tuple<position_t, set<position_t>>> queue; | ||
| 119 | |||
| 120 | queue.emplace(start, set<position_t>{}); | ||
| 121 | |||
| 122 | size_t longest = 0; | ||
| 123 | while ( !queue.empty() ) { | ||
| 124 | auto [current_position, seen] = queue.front(); | ||
| 125 | queue.pop(); | ||
| 126 | |||
| 127 | while ( current_position != end ) { | ||
| 128 | seen.emplace(current_position); | ||
| 129 | |||
| 130 | const auto neighbours = get_neighbours(maze, current_position); | ||
| 131 | |||
| 132 | vector<position_t> next_positions; | ||
| 133 | |||
| 134 | // { neighbours \ seen } | ||
| 135 | set_difference(neighbours.begin(), neighbours.end(), | ||
| 136 | seen.begin(), seen.end(), | ||
| 137 | back_inserter(next_positions)); | ||
| 138 | |||
| 139 | if ( next_positions.empty() ) { | ||
| 140 | break; | ||
| 141 | } | ||
| 142 | |||
| 143 | current_position = next_positions[0]; | ||
| 144 | |||
| 145 | for ( size_t idx = 1; idx != next_positions.size(); ++idx ) { | ||
| 146 | queue.emplace(next_positions[idx], seen); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | if ( current_position == end ) { | ||
| 150 | longest = max(longest, seen.size()); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | cout << longest << endl; | ||
| 154 | } | ||
| 155 | |||
| 156 | void | ||
| 157 | part2(const vector<string>& maze) | ||
| 158 | { | 114 | { |
| 159 | const position_t start = { 0, maze[0].find('.') }; | 115 | const position_t start = { 0, maze[0].find('.') }; |
| 160 | const position_t end = { maze.size() - 1, maze[maze.size() - 1].find('.') }; | 116 | const position_t end = { maze.size() - 1, maze[maze.size() - 1].find('.') }; |
| 161 | 117 | ||
| 162 | vector<vector<bool>> visited(maze.size(), vector<bool>(maze[0].size())); | 118 | vector<vector<bool>> visited(maze.size(), vector<bool>(maze[0].size())); |
| 163 | 119 | ||
| 164 | function<long(position_t, long)> findmax = [&](position_t position, long current) -> long { | 120 | function<long(position_t, long)> find_longest_path = [&](position_t position, long current) -> long { |
| 165 | const auto [row, col] = position; | 121 | const auto [row, col] = position; |
| 166 | 122 | ||
| 167 | if ( visited[row][col] ) { | 123 | if ( visited[row][col] ) { |
| @@ -173,16 +129,17 @@ part2(const vector<string>& maze) | |||
| 173 | } | 129 | } |
| 174 | 130 | ||
| 175 | long value = 0; | 131 | long value = 0; |
| 132 | |||
| 176 | visited[row][col] = true; | 133 | visited[row][col] = true; |
| 177 | for ( const auto& neighbour: get_neighbours2(maze, position) ) { | 134 | for ( const auto& neighbour: neighbours(maze, position) ) { |
| 178 | value = max(value, findmax(neighbour, current + 1)); | 135 | value = max(value, find_longest_path(neighbour, current + 1)); |
| 179 | } | 136 | } |
| 180 | visited[row][col] = false; | 137 | visited[row][col] = false; |
| 181 | 138 | ||
| 182 | return value; | 139 | return value; |
| 183 | }; | 140 | }; |
| 184 | 141 | ||
| 185 | auto value = findmax(start, 0); | 142 | auto value = find_longest_path(start, 0); |
| 186 | cout << value << endl; | 143 | cout << value << endl; |
| 187 | } | 144 | } |
| 188 | 145 | ||
| @@ -190,6 +147,6 @@ int | |||
| 190 | main() | 147 | main() |
| 191 | { | 148 | { |
| 192 | const auto maze = read_file("data/day23.txt"); | 149 | const auto maze = read_file("data/day23.txt"); |
| 193 | part1(maze); | 150 | solve(maze, get_neighbours); |
| 194 | part2(maze); | 151 | solve(maze, get_neighbours2); |
| 195 | } | 152 | } |
