Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.stream.Collectors;
- public class Main {
- private static HashMap<Integer, List<String>> wordList = new HashMap<>();
- public static void main(String[] args) throws IOException{
- List<String> lines = readLines("words.txt");
- for(String s : lines){
- if(!wordList.containsKey(s.length())){
- wordList.put(s.length(), new ArrayList<>());
- }
- wordList.get(s.length()).add(s);
- }
- HashMap<Character, Character> table = new HashMap<>();
- table.put('a', 'h');
- table.put('o', 'd');
- System.out.println(decrypt("IAL FTNHPL PDDI DR RDNP WF IUD", table));
- table.clear();
- table.put('x', 's');
- System.out.println(decrypt("JNOH MALAJJGJ SLNOGQ JSOGX", table));
- }
- public static List<String> decrypt(String message, HashMap<Character, Character> table){
- message = message.toLowerCase();
- String[] words = message.split(" ");
- HashMap<Integer, List<String>> invalidated = new HashMap<>();
- HashMap<Integer, List<Character>> allTests = new HashMap<>();
- HashMap<Integer, Integer> toRemove = new HashMap<>();
- HashMap<Integer, Long> weight = new HashMap<>();
- List<String> sentences = new ArrayList<>();
- long totalPossible = 1;
- for(int i = 0; i < words.length; i++){
- toRemove.put(i, getAvailable(words[i], table).size());
- }
- for(int i = words.length - 1; i >= 0; i--){
- weight.put(i, totalPossible);
- totalPossible *= toRemove.get(i);
- System.out.println(totalPossible);
- }
- System.out.println(totalPossible);
- loop: while(totalPossible > 1000){
- for(int x = 0; x < words.length; x++){
- if(!invalidated.containsKey(x)){
- invalidated.put(x, new ArrayList<>());
- }
- if(!allTests.containsKey(x)){
- allTests.put(x, new ArrayList<>());
- }
- String w = words[x];
- List<Character> testing = new ArrayList<>();
- List<String> available = getAvailable(w, table);
- boolean allInvalidated = true;
- boolean found = false;
- for(String p : available){
- if(invalidated.get(x).contains(p)){
- continue;
- }
- allInvalidated = false;
- boolean ok = true;
- for(int i = 0; i < w.length(); i++){
- char ic = w.charAt(i);
- char pc = p.charAt(i);
- if((!table.containsKey(ic) && !table.containsValue(pc)) || (table.containsKey(ic) && table.get(ic) == pc)){
- if(!table.containsKey(ic)){
- testing.add(ic);
- table.put(ic, pc);
- }
- }else{
- testing.forEach(table::remove);
- testing.clear();
- ok = false;
- break;
- }
- }
- if(ok){
- found = true;
- break;
- }
- }
- allTests.get(x).addAll(testing);
- if(!validateWords(words, table) || !found){
- if(allInvalidated || !found){
- invalidated.get(x).clear();
- if(x == 0){
- break loop;
- }
- invalidated.get(x - 1).add(replaceWithTable(words[x - 1], table));
- allTests.get(x - 1).forEach(table::remove);
- allTests.get(x - 1).clear();
- x -= 2;
- }else{
- totalPossible -= weight.get(x);
- invalidated.get(x).add(replaceWithTable(w, table));
- x--;
- }
- testing.forEach(table::remove);
- testing.clear();
- }
- }
- System.out.println(replaceWithTable(message, table).replace('.', ' '));
- sentences.add(replaceWithTable(message, table).replace('.', ' '));
- invalidated.get(words.length - 1).add(replaceWithTable(words[words.length - 1], table));
- allTests.get(words.length - 1).forEach(table::remove);
- allTests.get(words.length - 1).clear();
- }
- return sentences;
- }
- public static boolean validateWords(String[] words, HashMap<Character, Character> table){
- for(String word : words){
- List<String> available = getAvailable(word, table);
- if(available.size() == 0){
- return false;
- }
- }
- return true;
- }
- public static List<String> getAvailable(String word, HashMap<Character, Character> table){
- List<String> possible = wordList.get(word.length());
- List<String> available = new ArrayList<>();
- String replaced = replaceWithTable(word, table);
- available.addAll(possible.stream().filter(s -> s.matches(replaced)).collect(Collectors.toList()));
- return available;
- }
- public static String replaceWithTable(String word, HashMap<Character, Character> table){
- for(int i = 0; i < word.length(); i++){
- if(table.containsKey(word.charAt(i))){
- word = replace(word, i, table.get(word.charAt(i)));
- }else{
- word = replace(word, i, '.');
- }
- }
- return word;
- }
- public static String replace(String s, int index, char replace){
- if(s == null){
- return s;
- }else if(index < 0 || index >= s.length()){
- return s;
- }
- char[] chars = s.toCharArray();
- chars[index] = replace;
- return String.valueOf(chars);
- }
- public static List<String> readLines(String file) throws IOException{
- return Files.readAllLines(new File(file).toPath());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement