Skip to content

Instantly share code, notes, and snippets.

@imtsuki
Created September 14, 2022 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imtsuki/1f2d2bf4004fe4081ed1960ac46abbce to your computer and use it in GitHub Desktop.
Save imtsuki/1f2d2bf4004fe4081ed1960ac46abbce to your computer and use it in GitHub Desktop.
Decode Zonai Scripts
#include <fstream>
#include <iostream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
vector<string> ciphers;
vector<string> words;
void dfs(int i, unordered_map<char, char> cipher_map) {
for (int j = 0; j < i; j++) {
auto &cipher = ciphers[j];
for (auto &c : cipher) {
cout << cipher_map[c];
}
cout << " ";
}
cout << endl;
if (i == ciphers.size()) {
return;
}
auto &cipher = ciphers[i];
for (auto &word : words) {
unordered_map<char, char> new_cipher_map = cipher_map;
if (word.size() != cipher.size()) {
continue;
}
bool ok = true;
for (int j = 0; j < word.size(); j++) {
if (new_cipher_map.count(cipher[j])) {
if (new_cipher_map[cipher[j]] != word[j]) {
ok = false;
}
} else {
new_cipher_map[cipher[j]] = word[j];
}
}
if (ok) {
dfs(i + 1, new_cipher_map);
}
}
}
int main() {
string word;
ifstream is("words_alpha.txt");
while (getline(is, word)) {
words.push_back(word);
}
is.close();
// ABCDEB
// EFAGHI
// GHDIEJH
// FICHDCH
// KCHDJFI
// FICHDKC
ciphers.push_back("FICHDCH");
ciphers.push_back("FICHDKC");
ciphers.push_back("KCHDJFI");
ciphers.push_back("ABCDEB");
ciphers.push_back("EFAGHI");
ciphers.push_back("GHDIEJH");
unordered_map<char, char> cipher_map;
dfs(0, cipher_map);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment