|
| 1 | +#include <algorithm> |
| 2 | +#include <fstream> |
| 3 | +#include <iostream> |
| 4 | +#include <string> |
| 5 | +#include <ranges> |
| 6 | +#include <vector> |
| 7 | +#include <unordered_map> |
| 8 | + |
| 9 | +int sv_to_int (const std::string_view sv) { |
| 10 | + bool negative = (sv[0] == '-') ? true : false; |
| 11 | + int n = 0; |
| 12 | + for (int i = negative ? 1 : 0; i < sv.size(); i++) { |
| 13 | + n = n * 10 + (sv[i] - '0'); |
| 14 | + } |
| 15 | + if (negative) { |
| 16 | + n *= -1; |
| 17 | + } |
| 18 | + return n; |
| 19 | +} |
| 20 | + |
| 21 | +int main(int argc, char* argv[]) { |
| 22 | + std::string input = "../input/day_21_input"; |
| 23 | + if (argc > 1) { |
| 24 | + input = argv[1]; |
| 25 | + } |
| 26 | + |
| 27 | + std::ifstream file(input); |
| 28 | + std::string line; |
| 29 | + std::unordered_map<char, int> char_to_idx; |
| 30 | + std::unordered_map<int, char> idx_to_char; |
| 31 | + |
| 32 | + std::vector<std::vector<std::string>> instructions; |
| 33 | + std::string p = "abcdefgh"; |
| 34 | + std::string result = "00000000"; |
| 35 | + const int n = p.size(); |
| 36 | + |
| 37 | + while(std::getline(file, line)) { |
| 38 | + // std::cout << line << '\n'; |
| 39 | + std::vector<std::string> split_line; |
| 40 | + std::ranges::copy(line |
| 41 | + | std::ranges::views::split(' ') |
| 42 | + | std::ranges::views::transform([](auto&& rng) {return std::string{std::string_view(rng)};}), |
| 43 | + std::back_inserter(split_line)); |
| 44 | + instructions.push_back(split_line); |
| 45 | + } |
| 46 | + // Assumes a valid permutation will be found |
| 47 | + while(true){ |
| 48 | + // std::cout << p << '\n'; |
| 49 | + for (int i = 0; i <= p.size(); i++) { |
| 50 | + char_to_idx[p[i]] = i; |
| 51 | + idx_to_char[i] = p[i]; |
| 52 | + } |
| 53 | + for (const auto& split_line : instructions) { |
| 54 | + if (split_line[0] == "swap" && split_line[1] == "position") { |
| 55 | + const int idx_1 = sv_to_int(split_line[2]); |
| 56 | + const int idx_2 = sv_to_int(split_line[5]); |
| 57 | + const char c1 = idx_to_char[idx_1]; |
| 58 | + const char c2 = idx_to_char[idx_2]; |
| 59 | + idx_to_char[idx_1] = c2; |
| 60 | + idx_to_char[idx_2] = c1; |
| 61 | + char_to_idx[c1] = idx_2; |
| 62 | + char_to_idx[c2] = idx_1; |
| 63 | + } |
| 64 | + else if (split_line[0] == "swap" && split_line[1] == "letter") { |
| 65 | + const char c1 = split_line[2][0]; |
| 66 | + const char c2 = split_line[5][0]; |
| 67 | + const int idx_1 = char_to_idx[c1]; |
| 68 | + const int idx_2 = char_to_idx[c2]; |
| 69 | + idx_to_char[idx_1] = c2; |
| 70 | + idx_to_char[idx_2] = c1; |
| 71 | + char_to_idx[c1] = idx_2; |
| 72 | + char_to_idx[c2] = idx_1; |
| 73 | + } |
| 74 | + else if (split_line[0] == "rotate" && split_line[1] == "left") { |
| 75 | + const int rotate_n = (-sv_to_int(split_line[2]) + n) % n; |
| 76 | + for (auto& [c, idx] : char_to_idx) { |
| 77 | + idx = (idx + rotate_n) % n; |
| 78 | + idx_to_char[idx] = c; |
| 79 | + } |
| 80 | + } |
| 81 | + else if (split_line[0] == "rotate" && split_line[1] == "right") { |
| 82 | + const int rotate_n = sv_to_int(split_line[2]) % n; |
| 83 | + for (auto& [c, idx] : char_to_idx) { |
| 84 | + idx = (idx + rotate_n) % n; |
| 85 | + idx_to_char[idx] = c; |
| 86 | + } |
| 87 | + } |
| 88 | + else if (split_line[0] == "rotate" && split_line[1] == "based") { |
| 89 | + const auto c_idx = char_to_idx[split_line[6][0]]; |
| 90 | + const int rotate_n = 1 + c_idx + ((c_idx >= 4) ? 1 : 0); |
| 91 | + for (auto& [c, idx] : char_to_idx) { |
| 92 | + idx = (idx + rotate_n + n) % n; |
| 93 | + idx_to_char[idx] = c; |
| 94 | + } |
| 95 | + } |
| 96 | + else if (split_line[0] == "reverse") { |
| 97 | + const int idx_1 = sv_to_int(split_line[2]); |
| 98 | + const int idx_2 = sv_to_int(split_line[4]); |
| 99 | + for (auto& [c, idx] : char_to_idx) { |
| 100 | + if (idx >= idx_1 && idx <= idx_2) { |
| 101 | + idx = idx_2 - idx + idx_1; |
| 102 | + idx_to_char[idx] = c; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + else if (split_line[0] == "move" && split_line[1] == "position") { |
| 107 | + const int idx_1 = sv_to_int(split_line[2]); |
| 108 | + const int idx_2 = sv_to_int(split_line[5]); |
| 109 | + char c_prime = idx_to_char[idx_1]; |
| 110 | + if (idx_2 > idx_1) { |
| 111 | + for (auto& [c, idx] : char_to_idx) { |
| 112 | + if (idx > idx_1 && idx <= idx_2) { |
| 113 | + idx--; |
| 114 | + idx_to_char[idx] = c; |
| 115 | + } |
| 116 | + } |
| 117 | + char_to_idx[c_prime] = idx_2; |
| 118 | + idx_to_char[idx_2] = c_prime; |
| 119 | + } else if (idx_1 > idx_2) { |
| 120 | + for (auto& [c, idx] : char_to_idx) { |
| 121 | + if (idx >= idx_2 && idx < idx_1) { |
| 122 | + idx++; |
| 123 | + idx_to_char[idx] = c; |
| 124 | + } |
| 125 | + } |
| 126 | + char_to_idx[c_prime] = idx_2; |
| 127 | + idx_to_char[idx_2] = c_prime; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + for (const auto [c, idx] : char_to_idx) { |
| 132 | + result[idx] = c; |
| 133 | + } |
| 134 | + if (result == "fbgdceah") { |
| 135 | + std::cout << p << '\n'; |
| 136 | + return 0; |
| 137 | + } |
| 138 | + std::next_permutation(p.begin(), p.end()); |
| 139 | + } |
| 140 | + std::cout << "No valid permutation found" << '\n'; |
| 141 | + return 0; |
| 142 | +} |
0 commit comments