Advertisement
Vilsol

DailyProgrammer #220 - Hard

Jun 27th, 2015
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.45 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.nio.file.Files;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.stream.Collectors;
  8.  
  9. public class Main {
  10.  
  11.     private static HashMap<Integer, List<String>> wordList = new HashMap<>();
  12.  
  13.     public static void main(String[] args) throws IOException{
  14.         List<String> lines = readLines("words.txt");
  15.         for(String s : lines){
  16.             if(!wordList.containsKey(s.length())){
  17.                 wordList.put(s.length(), new ArrayList<>());
  18.             }
  19.             wordList.get(s.length()).add(s);
  20.         }
  21.  
  22.         HashMap<Character, Character> table = new HashMap<>();
  23.         table.put('a', 'h');
  24.         table.put('o', 'd');
  25.  
  26.         System.out.println(decrypt("IAL FTNHPL PDDI DR RDNP WF IUD", table));
  27.  
  28.         table.clear();
  29.         table.put('x', 's');
  30.  
  31.         System.out.println(decrypt("JNOH MALAJJGJ SLNOGQ JSOGX", table));
  32.     }
  33.  
  34.     public static List<String> decrypt(String message, HashMap<Character, Character> table){
  35.         message = message.toLowerCase();
  36.         String[] words = message.split(" ");
  37.         HashMap<Integer, List<String>> invalidated = new HashMap<>();
  38.         HashMap<Integer, List<Character>> allTests = new HashMap<>();
  39.         HashMap<Integer, Integer> toRemove = new HashMap<>();
  40.         HashMap<Integer, Long> weight = new HashMap<>();
  41.         List<String> sentences = new ArrayList<>();
  42.  
  43.         long totalPossible = 1;
  44.         for(int i = 0; i < words.length; i++){
  45.             toRemove.put(i, getAvailable(words[i], table).size());
  46.         }
  47.  
  48.         for(int i = words.length - 1; i >= 0; i--){
  49.             weight.put(i, totalPossible);
  50.             totalPossible *= toRemove.get(i);
  51.             System.out.println(totalPossible);
  52.         }
  53.  
  54.         System.out.println(totalPossible);
  55.  
  56.         loop: while(totalPossible > 1000){
  57.             for(int x = 0; x < words.length; x++){
  58.                 if(!invalidated.containsKey(x)){
  59.                     invalidated.put(x, new ArrayList<>());
  60.                 }
  61.  
  62.                 if(!allTests.containsKey(x)){
  63.                     allTests.put(x, new ArrayList<>());
  64.                 }
  65.  
  66.                 String w = words[x];
  67.                 List<Character> testing = new ArrayList<>();
  68.                 List<String> available = getAvailable(w, table);
  69.                 boolean allInvalidated = true;
  70.                 boolean found = false;
  71.                 for(String p : available){
  72.                     if(invalidated.get(x).contains(p)){
  73.                         continue;
  74.                     }
  75.  
  76.                     allInvalidated = false;
  77.  
  78.                     boolean ok = true;
  79.                     for(int i = 0; i < w.length(); i++){
  80.                         char ic = w.charAt(i);
  81.                         char pc = p.charAt(i);
  82.                         if((!table.containsKey(ic) && !table.containsValue(pc)) || (table.containsKey(ic) && table.get(ic) == pc)){
  83.                             if(!table.containsKey(ic)){
  84.                                 testing.add(ic);
  85.                                 table.put(ic, pc);
  86.                             }
  87.                         }else{
  88.                             testing.forEach(table::remove);
  89.                             testing.clear();
  90.                             ok = false;
  91.                             break;
  92.                         }
  93.                     }
  94.  
  95.                     if(ok){
  96.                         found = true;
  97.                         break;
  98.                     }
  99.                 }
  100.  
  101.                 allTests.get(x).addAll(testing);
  102.  
  103.                 if(!validateWords(words, table) || !found){
  104.                     if(allInvalidated || !found){
  105.                         invalidated.get(x).clear();
  106.  
  107.                         if(x == 0){
  108.                             break loop;
  109.                         }
  110.  
  111.                         invalidated.get(x - 1).add(replaceWithTable(words[x - 1], table));
  112.  
  113.                         allTests.get(x - 1).forEach(table::remove);
  114.                         allTests.get(x - 1).clear();
  115.  
  116.                         x -= 2;
  117.                     }else{
  118.                         totalPossible -= weight.get(x);
  119.                         invalidated.get(x).add(replaceWithTable(w, table));
  120.                         x--;
  121.                     }
  122.  
  123.                     testing.forEach(table::remove);
  124.                     testing.clear();
  125.                 }
  126.             }
  127.  
  128.             System.out.println(replaceWithTable(message, table).replace('.', ' '));
  129.             sentences.add(replaceWithTable(message, table).replace('.', ' '));
  130.  
  131.             invalidated.get(words.length - 1).add(replaceWithTable(words[words.length - 1], table));
  132.  
  133.             allTests.get(words.length - 1).forEach(table::remove);
  134.             allTests.get(words.length - 1).clear();
  135.         }
  136.  
  137.         return sentences;
  138.     }
  139.  
  140.     public static boolean validateWords(String[] words, HashMap<Character, Character> table){
  141.         for(String word : words){
  142.             List<String> available = getAvailable(word, table);
  143.             if(available.size() == 0){
  144.                 return false;
  145.             }
  146.         }
  147.         return true;
  148.     }
  149.  
  150.     public static List<String> getAvailable(String word, HashMap<Character, Character> table){
  151.         List<String> possible = wordList.get(word.length());
  152.         List<String> available = new ArrayList<>();
  153.         String replaced = replaceWithTable(word, table);
  154.         available.addAll(possible.stream().filter(s -> s.matches(replaced)).collect(Collectors.toList()));
  155.         return available;
  156.     }
  157.  
  158.     public static String replaceWithTable(String word, HashMap<Character, Character> table){
  159.         for(int i = 0; i < word.length(); i++){
  160.             if(table.containsKey(word.charAt(i))){
  161.                 word = replace(word, i, table.get(word.charAt(i)));
  162.             }else{
  163.                 word = replace(word, i, '.');
  164.             }
  165.         }
  166.         return word;
  167.     }
  168.  
  169.     public static String replace(String s, int index, char replace){
  170.         if(s == null){
  171.             return s;
  172.         }else if(index < 0 || index >= s.length()){
  173.             return s;
  174.         }
  175.         char[] chars = s.toCharArray();
  176.         chars[index] = replace;
  177.         return String.valueOf(chars);
  178.     }
  179.  
  180.     public static List<String> readLines(String file) throws IOException{
  181.         return Files.readAllLines(new File(file).toPath());
  182.     }
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement