|
| 1 | +#include <algorithm> |
| 2 | +#include <functional> |
| 3 | +#include <map> |
| 4 | +#include <optional> |
| 5 | +#include <sstream> |
| 6 | +#include <string> |
| 7 | +#include <unordered_set> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +#include "alphametics.h" |
| 11 | + |
| 12 | +namespace alphametics { |
| 13 | + |
| 14 | +std::optional<std::map<char, int>> solve(const std::string& puzzle) { |
| 15 | + auto sep = puzzle.find("=="); |
| 16 | + if (sep == std::string::npos) return std::nullopt; |
| 17 | + std::string left = puzzle.substr(0, sep); |
| 18 | + std::string right = puzzle.substr(sep + 2); |
| 19 | + |
| 20 | + auto trim = [&](std::string& s) { |
| 21 | + auto b = s.find_first_not_of(' '); |
| 22 | + auto e = s.find_last_not_of(' '); |
| 23 | + s = (b == std::string::npos) ? std::string() : s.substr(b, e - b + 1); |
| 24 | + }; |
| 25 | + trim(left); |
| 26 | + trim(right); |
| 27 | + |
| 28 | + std::vector<std::string> addends; |
| 29 | + std::istringstream iss(left); |
| 30 | + for (std::string part; std::getline(iss, part, '+');) { |
| 31 | + trim(part); |
| 32 | + addends.push_back(part); |
| 33 | + } |
| 34 | + trim(right); |
| 35 | + std::string result = right; |
| 36 | + |
| 37 | + std::vector<char> letters; |
| 38 | + std::unordered_set<char> seen, leading; |
| 39 | + auto collect = [&](const std::string& w) { |
| 40 | + if (w.size() > 1) leading.insert(w.front()); |
| 41 | + for (char c : w) { |
| 42 | + if (seen.insert(c).second) letters.push_back(c); |
| 43 | + } |
| 44 | + }; |
| 45 | + for (auto& w : addends) collect(w); |
| 46 | + collect(result); |
| 47 | + if (letters.size() > 10) return std::nullopt; |
| 48 | + |
| 49 | + int n = letters.size(); |
| 50 | + std::map<char, int> idx; |
| 51 | + for (int i = 0; i < n; ++i) idx[letters[i]] = i; |
| 52 | + std::vector<long long> weight(n, 0); |
| 53 | + |
| 54 | + for (auto& w : addends) { |
| 55 | + long long mult = 1; |
| 56 | + for (int i = (int)w.size() - 1; i >= 0; --i) { |
| 57 | + weight[idx[w[i]]] += mult; |
| 58 | + mult *= 10; |
| 59 | + } |
| 60 | + } |
| 61 | + { |
| 62 | + long long mult = 1; |
| 63 | + for (int i = (int)result.size() - 1; i >= 0; --i) { |
| 64 | + weight[idx[result[i]]] -= mult; |
| 65 | + mult *= 10; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + std::vector<bool> used(10, false); |
| 70 | + std::map<char, int> assign; |
| 71 | + bool found = false; |
| 72 | + std::map<char, int> solution; |
| 73 | + std::function<void(int, long long)> dfs = [&](int pos, long long sum) { |
| 74 | + if (found) return; |
| 75 | + if (pos == n) { |
| 76 | + if (sum == 0) { |
| 77 | + found = true; |
| 78 | + solution = assign; |
| 79 | + } |
| 80 | + return; |
| 81 | + } |
| 82 | + char c = letters[pos]; |
| 83 | + for (int d = 0; d < 10 && !found; ++d) { |
| 84 | + if (used[d] || (d == 0 && leading.count(c))) continue; |
| 85 | + used[d] = true; |
| 86 | + assign[c] = d; |
| 87 | + dfs(pos + 1, sum + weight[pos] * d); |
| 88 | + used[d] = false; |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + dfs(0, 0); |
| 93 | + if (found) return solution; |
| 94 | + return std::nullopt; |
| 95 | +} |
| 96 | + |
| 97 | +} // namespace alphametics |
0 commit comments