Advertisement
punpun1000

Self Referential Puzzle check

Apr 19th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.44 KB | None | 0 0
  1. import java.lang.reflect.Method;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5. public class Scratch {
  6.     public static void main(String[] args) throws Exception {
  7.         List<String> valid = new ArrayList<String>();
  8.  
  9.         for(String s : getStrings("")) {
  10.             if(checkString(s)) {
  11.                 valid.add(s);
  12.             }
  13.         }
  14.         if(valid.isEmpty()) {
  15.             System.out.println("No valid");
  16.         }
  17.         for(String s : valid) {
  18.             System.out.println(s);
  19.         }
  20.     }
  21.  
  22.     public static List<String> getStrings(String s) {
  23.         List<String> r = new ArrayList<String>();
  24.         if(s.length() == 10) {
  25.             r.add(s);
  26.             return r;
  27.         }
  28.  
  29.         for(char c = 'A'; c <= 'E'; c++) {
  30.             r.addAll(getStrings(s + c));
  31.         }
  32.         return r;
  33.     }
  34.     public static boolean checkString(String s) throws Exception {
  35.         for(int i = 0; i < 10; i++) {
  36.             char letter = s.charAt(i);
  37.             Method m = Scratch.class.getMethod("Q" + i + letter, String.class);
  38.             boolean b = (boolean)m.invoke(null, s);
  39.             if(!b) {
  40.                 return false;
  41.             }
  42.         }
  43.         return true;
  44.     }
  45.  
  46.     public static int substringCount(String s, String sub) {
  47.         int index = s.indexOf(sub);
  48.         int count = 0;
  49.        
  50.         while (index != -1) {
  51.             count++;
  52.             s = s.substring(index + 1);
  53.             index = s.indexOf(sub);
  54.         }
  55.  
  56.         return count;
  57.     }
  58.  
  59.     public static boolean Q0A(String s) {
  60.         return s.indexOf("D") == 7;
  61.     }
  62.  
  63.     public static boolean Q0B(String s) {
  64.         return s.indexOf("D") == 6;
  65.     }
  66.  
  67.     public static boolean Q0C(String s) {
  68.         return s.indexOf("D") == 5;
  69.     }
  70.  
  71.     public static boolean Q0D(String s) {
  72.         return s.indexOf("D") == 4;
  73.     }
  74.  
  75.     public static boolean Q0E(String s) {
  76.         return s.indexOf("D") == 3;
  77.     }
  78.  
  79.     public static boolean Q1A(String s) {
  80.         return s.charAt(2) == s.charAt(3);
  81.     }
  82.  
  83.     public static boolean Q1B(String s) {
  84.         return s.charAt(3) == s.charAt(4);
  85.     }
  86.  
  87.     public static boolean Q1C(String s) {
  88.         return s.charAt(4) == s.charAt(5);
  89.     }
  90.  
  91.     public static boolean Q1D(String s) {
  92.         return s.charAt(5) == s.charAt(6);
  93.     }
  94.  
  95.     public static boolean Q1E(String s) {
  96.         return s.charAt(6) == s.charAt(7);
  97.     }
  98.  
  99.     public static boolean Q2A(String s) {
  100.         return substringCount(s, "E") == 1;
  101.     }
  102.  
  103.     public static boolean Q2B(String s) {
  104.         return substringCount(s, "E") == 2;
  105.     }
  106.  
  107.     public static boolean Q2C(String s) {
  108.         return substringCount(s, "E") == 3;
  109.     }
  110.  
  111.     public static boolean Q2D(String s) {
  112.         return substringCount(s, "E") == 4;
  113.     }
  114.  
  115.     public static boolean Q2E(String s) {
  116.         return substringCount(s, "E") == 5;
  117.     }
  118.  
  119.     public static boolean Q3A(String s) {
  120.         return substringCount(s, "A") == 1;
  121.     }
  122.  
  123.     public static boolean Q3B(String s) {
  124.         return substringCount(s, "A") == 2;
  125.     }
  126.  
  127.     public static boolean Q3C(String s) {
  128.         return substringCount(s, "A") == 3;
  129.     }
  130.  
  131.     public static boolean Q3D(String s) {
  132.         return substringCount(s, "A") == 4;
  133.     }
  134.  
  135.     public static boolean Q3E(String s) {
  136.         return substringCount(s, "A") == 5;
  137.     }
  138.  
  139.     public static boolean Q4A(String s) {
  140.         return true;
  141.     }
  142.  
  143.     public static boolean Q4B(String s) {
  144.         return substringCount(s, "A") == substringCount(s, "B");
  145.     }
  146.  
  147.     public static boolean Q4C(String s) {
  148.         return substringCount(s, "A") == substringCount(s, "C");
  149.     }
  150.  
  151.     public static boolean Q4D(String s) {
  152.         return substringCount(s, "A") == substringCount(s, "D");
  153.     }
  154.  
  155.     public static boolean Q4E(String s) {
  156.         return false;
  157.     }
  158.  
  159.     public static boolean Q5A(String s) {
  160.         return s.lastIndexOf("B") == 4;
  161.     }
  162.  
  163.     public static boolean Q5B(String s) {
  164.         return s.lastIndexOf("B") == 5;
  165.     }
  166.  
  167.     public static boolean Q5C(String s) {
  168.         return s.lastIndexOf("B") == 6;
  169.     }
  170.  
  171.     public static boolean Q5D(String s) {
  172.         return s.lastIndexOf("B") == 7;
  173.     }
  174.  
  175.     public static boolean Q5E(String s) {
  176.         return s.lastIndexOf("B") == 8;
  177.     }
  178.  
  179.     public static boolean Q6A(String s) {
  180.         return Math.abs((int)s.charAt(6) - (int)s.charAt(7)) == 4;
  181.     }
  182.  
  183.     public static boolean Q6B(String s) {
  184.         return Math.abs((int)s.charAt(6) - (int)s.charAt(7)) == 3;
  185.     }
  186.  
  187.     public static boolean Q6C(String s) {
  188.         return Math.abs((int)s.charAt(6) - (int)s.charAt(7)) == 2;
  189.     }
  190.  
  191.     public static boolean Q6D(String s) {
  192.         return Math.abs((int)s.charAt(6) - (int)s.charAt(7)) == 1;
  193.     }
  194.  
  195.     public static boolean Q6E(String s) {
  196.         return s.charAt(6) == s.charAt(7);
  197.     }
  198.  
  199.     public static boolean Q7A(String s) {
  200.         return s.charAt(7) == s.charAt(0);
  201.     }
  202.  
  203.     public static boolean Q7B(String s) {
  204.         return s.charAt(7) == s.charAt(1);
  205.     }
  206.  
  207.     public static boolean Q7C(String s) {
  208.         return s.charAt(7) == s.charAt(2);
  209.     }
  210.  
  211.     public static boolean Q7D(String s) {
  212.         return s.charAt(7) == s.charAt(3);
  213.     }
  214.  
  215.     public static boolean Q7E(String s) {
  216.         return s.charAt(7) == s.charAt(4);
  217.     }
  218.  
  219.     public static boolean Q8A(String s) {
  220.         return (substringCount(s, "B") + substringCount(s, "C") + substringCount(s, "D")) == 3;
  221.     }
  222.  
  223.     public static boolean Q8B(String s) {
  224.         return (substringCount(s, "B") + substringCount(s, "C") + substringCount(s, "D")) == 4;
  225.     }
  226.  
  227.     public static boolean Q8C(String s) {
  228.         return (substringCount(s, "B") + substringCount(s, "C") + substringCount(s, "D")) == 5;
  229.     }
  230.  
  231.     public static boolean Q8D(String s) {
  232.         return (substringCount(s, "B") + substringCount(s, "C") + substringCount(s, "D")) == 6;
  233.     }
  234.  
  235.     public static boolean Q8E(String s) {
  236.         return (substringCount(s, "B") + substringCount(s, "C") + substringCount(s, "D")) == 7;
  237.     }
  238.  
  239.     public static boolean Q9A(String s) {
  240.         return true;
  241.     }
  242.  
  243.     public static boolean Q9B(String s) {
  244.         return true;
  245.     }
  246.  
  247.     public static boolean Q9C(String s) {
  248.         return true;
  249.     }
  250.  
  251.     public static boolean Q9D(String s) {
  252.         return true;
  253.     }
  254.  
  255.     public static boolean Q9E(String s) {
  256.         return true;
  257.     }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement