|
1 | 1 | #include "advent_of_code/day_6.hxx" |
| 2 | +#include "spdlog/spdlog.h" |
| 3 | +#include <algorithm> |
| 4 | +#include <iterator> |
| 5 | +#include <stdexcept> |
| 6 | +#include <string> |
| 7 | +#include <unordered_map> |
2 | 8 |
|
3 | 9 | namespace AdventOfCode24::Day6 { |
4 | | - std::pair<status, std::vector<coord>> get_area_map(const std::filesystem::path& input_file) { |
5 | | - |
6 | | - std::ifstream read_in(input_file, std::ios::in); |
7 | | - std::string line; |
8 | | - std::vector<coord> area_map; |
9 | | - status guard_init{{-1,-1},{0,0}}; |
10 | | - const char guard_symbols[4]{'v','>','<','^'}; |
11 | | - |
12 | | - |
13 | | - while (std::getline(read_in, line)) { |
14 | | - char symbol{'@'}; |
15 | | - std::istringstream iss; |
16 | | - iss.str(line); |
17 | | - int column{0} |
18 | | - |
19 | | - while(iss >> symbol) { |
| 10 | + coord new_direction(const coord& current_direction) { |
| 11 | + if(current_direction.first > 0) { |
| 12 | + return {0, -1}; |
| 13 | + } |
| 14 | + if(current_direction.first < 0) { |
| 15 | + return {0, 1}; |
| 16 | + } |
| 17 | + if(current_direction.second > 0) { |
| 18 | + return {1, 0}; |
| 19 | + } |
| 20 | + if(current_direction.second < 0) { |
| 21 | + return {-1, 0}; |
| 22 | + } |
| 23 | + |
| 24 | + throw std::runtime_error("Direction change failed for direction " + std::to_string(current_direction.first) + "," + std::to_string(current_direction.second)); |
| 25 | + } |
| 26 | + |
| 27 | + Map get_area_map(const std::filesystem::path& input_file) { |
| 28 | + std::ifstream read_in(input_file, std::ios::in); |
| 29 | + std::string line; |
| 30 | + Map area_map; |
| 31 | + |
| 32 | + const std::unordered_map<char, coord> directions{ |
| 33 | + {'^', {-1, 0}}, |
| 34 | + {'v', {1, 0}}, |
| 35 | + {'>', {0, 1}}, |
| 36 | + {'<', {0, -1}} |
| 37 | + }; |
| 38 | + |
| 39 | + int line_num{0}; |
| 40 | + |
| 41 | + while (std::getline(read_in, line)) { |
| 42 | + int column{0}; |
| 43 | + area_map.size.second = line.size(); |
| 44 | + |
| 45 | + for(const char& symbol : line) { |
| 46 | + if(symbol == '.') { |
| 47 | + column++; |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + // Firstly check if this is an obstruction location |
20 | 52 | if(symbol == '#') { |
21 | | - area_map.push_back({area_map.size(), column}); |
| 53 | + spdlog::debug("Found obstacle at [" + std::to_string(line_num) + "," + std::to_string(column) + "]"); |
| 54 | + area_map.obstacles.push_back({line_num, column}); |
22 | 55 | column++; |
23 | 56 | continue; |
24 | 57 | } |
25 | | - auto find_guard = std::find(std::begin(guard_symbols), std::end(guard_symbols), symbol); |
| 58 | + |
| 59 | + // Secondly check if this location is where the guard starts |
| 60 | + auto find_guard = directions.find(symbol); |
| 61 | + |
| 62 | + if(find_guard != directions.end()) { |
| 63 | + const coord start_dir{directions.at(symbol)}; |
| 64 | + std::string sym_str{symbol}; |
| 65 | + area_map.guard_direction = start_dir; |
| 66 | + area_map.guard_position = {line_num, column}; |
| 67 | + spdlog::debug("Found Guard at [" + std::to_string(line_num) + "," + std::to_string(column) + "]"); |
| 68 | + spdlog::debug("Guard facing direction '" + sym_str + "'"); |
| 69 | + } |
| 70 | + column++; |
| 71 | + } |
| 72 | + |
| 73 | + line_num++; |
| 74 | + } |
| 75 | + |
| 76 | + area_map.size.first = line_num; |
| 77 | + |
| 78 | + return area_map; |
| 79 | + } |
| 80 | + |
| 81 | + void show_result(const Map& map, const std::vector<coord>&path) { |
| 82 | + for(int row{0}; row < map.size.first; ++row) { |
| 83 | + for(int column{0}; column < map.size.second; ++column) { |
| 84 | + const coord current{row, column}; |
| 85 | + auto find_obstacle = std::find(map.obstacles.begin(), map.obstacles.end(), current); |
| 86 | + auto find_path = std::find(path.begin(), path.end(), current); |
| 87 | + |
| 88 | + if(row == map.obstacles[map.obstacles.size() - 1].first && column == map.obstacles[map.obstacles.size() - 1].second) { |
| 89 | + std::cout << "\x1B[34m■\033[0m"; |
| 90 | + } else if(find_obstacle != map.obstacles.end()) { |
| 91 | + std::cout << "\x1B[31m■\033[0m"; |
| 92 | + } else if(find_path != path.end()) { |
| 93 | + std::cout << "\x1B[32m■\033[0m"; |
| 94 | + } else { |
| 95 | + std::cout << "■"; |
| 96 | + } |
26 | 97 | } |
| 98 | + |
| 99 | + std::cout << std::endl; |
| 100 | + } |
| 101 | + std::cout << std::endl; |
| 102 | + } |
| 103 | + |
| 104 | + std::pair<ExitStatus, std::vector<coord>> get_path(const Map& map) { |
| 105 | + spdlog::debug("Guard starting at " + std::to_string(map.guard_position.first) + "," + std::to_string(map.guard_position.second)); |
| 106 | + coord current_pos{map.guard_position}; |
| 107 | + coord current_dir{map.guard_direction}; |
| 108 | + std::vector<coord> path{current_pos}; |
| 109 | + const int HARD_LIMIT{map.size.first * map.size.second}; |
| 110 | + |
| 111 | + while(true) { |
| 112 | + const coord next_coord{ |
| 113 | + current_pos.first + current_dir.first, |
| 114 | + current_pos.second + current_dir.second |
| 115 | + }; |
| 116 | + |
| 117 | + const coord next_coord_2{ |
| 118 | + next_coord.first + current_dir.first, |
| 119 | + next_coord.second + current_dir.second |
| 120 | + }; |
| 121 | + |
| 122 | + auto find_obstacle = std::find(map.obstacles.begin(), map.obstacles.end(), next_coord); |
| 123 | + |
| 124 | + auto find_next = std::find(path.begin(), path.end(), next_coord); |
| 125 | + auto find_next_2 = std::find(path.begin(), path.end(), next_coord_2); |
| 126 | + |
| 127 | + if(find_next != path.end() && find_next_2 != path.end()) { |
| 128 | + const int next_distance = std::distance(path.begin(), find_next); |
| 129 | + const int next_2_distance = std::distance(path.begin(), find_next_2); |
| 130 | + |
| 131 | + if(next_2_distance - next_distance == 1) { |
| 132 | + spdlog::warn("Loop detected aborting path."); |
| 133 | + return {ExitStatus::LoopDetected, path}; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + // Need to prevent infinite looping, if path size is comparable to grid size this is likely a loop |
| 138 | + if(path.size() > HARD_LIMIT) { |
| 139 | + spdlog::warn("Path size is unusually large, assuming loop detected and aborting path."); |
| 140 | + return {ExitStatus::LoopDetected, path}; |
| 141 | + } |
| 142 | + |
| 143 | + // Check if an obstacle was encountered |
| 144 | + if(find_obstacle != map.obstacles.end()) { |
| 145 | + spdlog::debug("Changing direction at " + std::to_string(current_pos.first) + "," + std::to_string(current_pos.second)); |
| 146 | + current_dir = new_direction(current_dir); |
| 147 | + } else if( |
| 148 | + current_pos.first < 0 || current_pos.first >= map.size.first || |
| 149 | + current_pos.second < 0 || current_pos.second >= map.size.second |
| 150 | + ) { |
| 151 | + spdlog::debug("Guard exiting map region at " + std::to_string(current_pos.first) + "," + std::to_string(current_pos.second)); |
| 152 | + path.pop_back(); |
| 153 | + break; |
| 154 | + } else { |
| 155 | + current_pos = next_coord; |
| 156 | + path.push_back(current_pos); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return {ExitStatus::ExitedMap, path}; |
| 161 | + } |
| 162 | + std::vector<coord> patrol_loop_obstruction_positions(const Map& map) { |
| 163 | + std::vector<coord> loopback_obs_positions; |
| 164 | + |
| 165 | + for(int row{0}; row < map.size.first; ++row) { |
| 166 | + spdlog::info("Process is " + std::to_string(1.0 * row / map.size.first * 100.0) + "% complete"); |
| 167 | + for(int column{0}; column < map.size.second; ++column) { |
| 168 | + const int total_cells{map.size.first * map.size.second}; |
| 169 | + spdlog::debug("Placing obstacle at " + std::to_string(row) + "," + std::to_string(column)); |
| 170 | + // Exclude guard position |
| 171 | + if(column == map.guard_position.second && row == map.guard_position.first) continue; |
| 172 | + |
| 173 | + const coord current{row, column}; |
| 174 | + |
| 175 | + // Exclude positions of current obstacles |
| 176 | + auto find_existing = std::find(map.obstacles.begin(), map.obstacles.end(), current); |
| 177 | + |
| 178 | + if(find_existing != map.obstacles.end()) continue; |
| 179 | + |
| 180 | + Map modified_map(map); |
| 181 | + modified_map.obstacles.push_back({row, column}); |
| 182 | + |
| 183 | + const std::pair<ExitStatus, std::vector<coord>> path_check{get_path(modified_map)}; |
| 184 | + |
| 185 | + if(path_check.first == ExitStatus::LoopDetected) { |
| 186 | + show_result(modified_map, path_check.second); |
| 187 | + loopback_obs_positions.push_back(current); |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + return loopback_obs_positions; |
27 | 193 | } |
28 | 194 | }; |
0 commit comments