|
| 1 | +#include <algorithm> |
| 2 | +#include <fstream> |
| 3 | +#include <iostream> |
| 4 | +#include <string> |
| 5 | +#include <queue> |
| 6 | +#include <ranges> |
| 7 | +#include <regex> |
| 8 | +#include <unordered_set> |
| 9 | +#include <vector> |
| 10 | +#include <iomanip> |
| 11 | + |
| 12 | +// Solve by hand. |
| 13 | +// Code to print map then replicate solve by hand solution |
| 14 | + |
| 15 | +struct Node { |
| 16 | + int x; |
| 17 | + int y; |
| 18 | + int size; |
| 19 | + int used; |
| 20 | + int avail; |
| 21 | + int p_used; |
| 22 | + std::string name; |
| 23 | +}; |
| 24 | + |
| 25 | +struct NodeMove { |
| 26 | + Node node; |
| 27 | + int move; |
| 28 | + bool operator == (const NodeMove& nm) const { |
| 29 | + return nm.node.x == node.x && nm.node.y == node.y; |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +struct NodeMoveHash { |
| 34 | + std::size_t operator () (const NodeMove& nm) const { |
| 35 | + return nm.node.x + nm.node.y * 100; |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +int sv_to_int (const std::string_view sv) { |
| 40 | + bool negative = (sv[0] == '-') ? true : false; |
| 41 | + int n = 0; |
| 42 | + for (int i = negative ? 1 : 0; i < sv.size(); i++) { |
| 43 | + n = n * 10 + (sv[i] - '0'); |
| 44 | + } |
| 45 | + if (negative) { |
| 46 | + n *= -1; |
| 47 | + } |
| 48 | + return n; |
| 49 | +} |
| 50 | + |
| 51 | +const std::array<Node, 4> moves { |
| 52 | + Node(0, 1, 0, 0, 0, 0, ""), |
| 53 | + Node(1, 0, 0, 0, 0, 0, ""), |
| 54 | + Node(0, -1, 0, 0, 0, 0, ""), |
| 55 | + Node(-1, 0, 0, 0, 0, 0, "") |
| 56 | +}; |
| 57 | + |
| 58 | +int search (const Node& start_node, const Node& goal_node, const std::vector<Node>& nodes, const int max_x, const int max_y) { |
| 59 | + NodeMove start; |
| 60 | + start.node = start_node; |
| 61 | + start.move = 0; |
| 62 | + std::unordered_set<NodeMove, NodeMoveHash> seen; |
| 63 | + std::queue<NodeMove> q; |
| 64 | + q.push(start); |
| 65 | + while (!q.empty()) { |
| 66 | + const auto current = q.front(); |
| 67 | + q.pop(); |
| 68 | + if (seen.find(current) != seen.end()) continue; |
| 69 | + seen.insert(current); |
| 70 | + if (current.node.x == goal_node.x && current.node.y == goal_node.y) { |
| 71 | + return current.move; |
| 72 | + } |
| 73 | + for (const auto& move : moves) { |
| 74 | + Node next; |
| 75 | + next.x = current.node.x + move.x; |
| 76 | + next.y = current.node.y + move.y; |
| 77 | + if (next.x < 0 || next.x > max_x || next.y < 0 || next.y > max_y) continue; |
| 78 | + next = *std::find_if(std::begin(nodes), std::end(nodes), [&next](const auto& node) {return node.x == next.x && node.y == next.y; }); |
| 79 | + if (next.used > 99) continue; // Assume obstacle |
| 80 | + NodeMove next_nm; |
| 81 | + next_nm.node = next; |
| 82 | + next_nm.move = current.move + 1; |
| 83 | + if (seen.find(next_nm) != seen.end()) continue; |
| 84 | + |
| 85 | + q.push(next_nm); |
| 86 | + } |
| 87 | + } |
| 88 | + return -1; |
| 89 | +} |
| 90 | + |
| 91 | +int main(int argc, char* argv[]) { |
| 92 | + std::string input = "../input/day_22_input"; |
| 93 | + if (argc > 1) { |
| 94 | + input = argv[1]; |
| 95 | + } |
| 96 | + |
| 97 | + std::ifstream file(input); |
| 98 | + std::string line; |
| 99 | + std::getline(file, line); |
| 100 | + std::getline(file, line); |
| 101 | + std::vector<Node> nodes; |
| 102 | + const std::regex pattern(R"((/dev/grid/node-x([0-9]+)-y([0-9]+))[ ]+([0-9]+)T[ ]+([0-9]+)T[ ]+([0-9]+)T[ ]+([0-9]+)%)"); |
| 103 | + int max_x = 0; |
| 104 | + int max_y = 0; |
| 105 | + while(std::getline(file, line)) { |
| 106 | + // std::cout << line << '\n'; |
| 107 | + std::smatch match; |
| 108 | + std::regex_search(line, match, pattern); |
| 109 | + // for(const auto ele : match) { |
| 110 | + // std::cout << ele << ' '; |
| 111 | + // } |
| 112 | + // std::cout << '\n'; |
| 113 | + Node node; |
| 114 | + node.name = match[1]; |
| 115 | + node.x = std::stoi(match[2]); |
| 116 | + max_x = std::max(node.x, max_x); |
| 117 | + node.y = std::stoi(match[3]); |
| 118 | + max_y = std::max(node.y, max_y); |
| 119 | + node.size = std::stoi(match[4]); |
| 120 | + node.used = std::stoi(match[5]); |
| 121 | + node.avail = std::stoi(match[6]); |
| 122 | + node.p_used = std::stoi(match[7]); |
| 123 | + nodes.push_back(node); |
| 124 | + } |
| 125 | + auto map = std::vector<std::vector<std::pair<int, int>>>(max_y+1, |
| 126 | + std::vector<std::pair<int, int>>( |
| 127 | + max_x+1, {0, 0} |
| 128 | + ) |
| 129 | + ); |
| 130 | + for (const auto& node : nodes) { |
| 131 | + map[node.y][node.x] = {node.used, node.size}; |
| 132 | + } |
| 133 | + for (const auto& row : map) { |
| 134 | + for (const auto& ele : row) { |
| 135 | + std::cout << std::setw(3) << ele.first ;//<< "|" << ele.second; |
| 136 | + } |
| 137 | + std::cout << '\n'; |
| 138 | + } |
| 139 | + |
| 140 | + Node empty; |
| 141 | + Node start; |
| 142 | + Node goal; |
| 143 | + for (const auto& node : nodes) { |
| 144 | + if (node.used == 0) { // This might be a small value? |
| 145 | + empty = node; |
| 146 | + } else if (node.x == max_x && node.y == 0) { |
| 147 | + start = node; |
| 148 | + } else if (node.x == 0 && node.y == 0) { |
| 149 | + goal = node; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + // Get the empty node to the top right corner |
| 154 | + // Then move the data in the top right corner to the top left |
| 155 | + // The assumption here is that to move the data one step, the empty node will need a minimum of 5 moves. |
| 156 | + // Start: | Move 1: | Move 2: | Move 3: | Move 4: | Move 5: | Repeat |
| 157 | + // . D E . | . D . . | . D . . | . D . . | E D . . | D E . . | |
| 158 | + // . . . . | . . E . | . E . . | E . . . | . . . . | . . . . | |
| 159 | + // Number of steps -1 as this is not necessary in the first move when moving data out of the top right corner |
| 160 | + // Assume that the minimum is always possible. |
| 161 | + std::cout << search(empty, start, nodes, max_x, max_y) + (search (start, goal, nodes, max_x, max_y) - 1 ) * 5 << '\n'; |
| 162 | + return 0; |
| 163 | +} |
| 164 | + |
0 commit comments