Advertisement
Guest User

EinsteinsRiddle.java

a guest
May 2nd, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.53 KB | None | 0 0
  1. package einsteinsriddle;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6.  
  7. /**
  8.  *
  9.  *
  10.  * @author Joel Brito
  11.  */
  12. public class EinsteinsRiddle {
  13.  
  14.     private enum Cor {
  15.  
  16.         VERMELHA,
  17.         AZUL,
  18.         VERDE,
  19.         MARFIM,
  20.         AMARELA
  21.     }
  22.  
  23.     private enum Nacionalidade {
  24.  
  25.         NORUEGUES,
  26.         INGLES,
  27.         ESPANHOL,
  28.         UCRANIANO,
  29.         JAPONES
  30.     }
  31.  
  32.     private enum Animal {
  33.  
  34.         CAO,
  35.         CARACOIS,
  36.         RAPOSA,
  37.         CAVALO,
  38.         ZEBRA
  39.     }
  40.  
  41.     private enum Bebida {
  42.  
  43.         CAFE,
  44.         CHA,
  45.         LEITE,
  46.         SUMO,
  47.         AGUA
  48.     }
  49.  
  50.     private enum Cigarro {
  51.  
  52.         OLDGOLD,
  53.         KOOLS,
  54.         CHESTERFIELDS,
  55.         LUCKYSTRIKE,
  56.         PARLIAMENTS
  57.     }
  58.  
  59.     /**
  60.      * @param args the command line arguments
  61.      */
  62.     public static void main(String[] args) {
  63.  
  64.  
  65.  
  66.         Object[] cores = new Object[]{Cor.AMARELA, Cor.AZUL, Cor.MARFIM, Cor.VERDE, Cor.VERMELHA};
  67.         Object[] nacionalidades = new Object[]{Nacionalidade.ESPANHOL, Nacionalidade.INGLES, Nacionalidade.JAPONES, Nacionalidade.NORUEGUES, Nacionalidade.UCRANIANO};
  68.         Object[] animais = new Object[]{Animal.CAO, Animal.CARACOIS, Animal.CAVALO, Animal.RAPOSA, Animal.ZEBRA};
  69.         Object[] bebidas = new Object[]{Bebida.AGUA, Bebida.CAFE, Bebida.CHA, Bebida.LEITE, Bebida.SUMO};
  70.         Object[] cigarros = new Object[]{Cigarro.CHESTERFIELDS, Cigarro.KOOLS, Cigarro.LUCKYSTRIKE, Cigarro.OLDGOLD, Cigarro.PARLIAMENTS};
  71.  
  72.         List<Object[]> permCores = new Permute().getPermutation(cores);
  73.         List<Object[]> permNacionalidades = new Permute().getPermutation(nacionalidades);
  74.         List<Object[]> permAnimais = new Permute().getPermutation(animais);
  75.         List<Object[]> permBebidas = new Permute().getPermutation(bebidas);
  76.         List<Object[]> permCigarros = new Permute().getPermutation(cigarros);
  77.  
  78.         int perms = 0;  //Conta o numero total de permutações existentes
  79.         int ciclo = 0;  //Para evitar o ciclo infinito se não encontrar solução
  80.  
  81.         //Para resolver o enigma as permutações tem que ser 5 no total
  82.         while (perms != 5 && ciclo < 50) {   //Apenas repete o ciclo 50 vezes
  83.  
  84.             List<Object[]> esq; //Guarda as permutações verificadas da esquerda da condição
  85.             List<Object[]> dir; //Guarda as permutações verificadas da direita da condição
  86.  
  87.             //2. O Ingês mora na casa vermelha
  88.             dir = new ArrayList<>();
  89.             esq = new ArrayList<>();
  90.             for (Object[] n : permNacionalidades) {
  91.                 for (Object[] c : permCores) {
  92.                     for (int i = 0; i < c.length; i++) {
  93.                         if (n[i] == (Object) Nacionalidade.INGLES && c[i] == (Object) Cor.VERMELHA) {
  94.                             if (!dir.contains(c)) {
  95.                                 dir.add(c);
  96.                             }
  97.                             if (!esq.contains(n)) {
  98.                                 esq.add(n);
  99.                             }
  100.                         }
  101.                     }
  102.                 }
  103.  
  104.             }
  105.             permCores = dir;
  106.             permNacionalidades = esq;
  107.  
  108.             //3. O Espanhol tem um cão.
  109.             dir = new ArrayList<>();
  110.             esq = new ArrayList<>();
  111.             for (Object[] n : permNacionalidades) {
  112.                 //count = 0;
  113.                 for (Object[] a : permAnimais) {
  114.                     for (int i = 0; i < a.length; i++) {
  115.                         if (n[i] == (Object) Nacionalidade.ESPANHOL && a[i] == (Object) Animal.CAO) {
  116.                             if (!dir.contains(a)) {
  117.                                 dir.add(a);
  118.                             }
  119.                             if (!esq.contains(n)) {
  120.                                 esq.add(n);
  121.                             }
  122.                         }
  123.                     }
  124.                 }
  125.  
  126.             }
  127.             permAnimais = dir;
  128.             permNacionalidades = esq;
  129.  
  130.             //4. O café é bebido na casa verde.
  131.             dir = new ArrayList<>();
  132.             esq = new ArrayList<>();
  133.             for (Object[] b : permBebidas) {
  134.                 for (Object[] c : permCores) {
  135.                     for (int i = 0; i < c.length; i++) {
  136.                         if (b[i] == (Object) Bebida.CAFE && c[i] == (Object) Cor.VERDE) {
  137.                             if (!dir.contains(c)) {
  138.                                 dir.add(c);
  139.                             }
  140.                             if (!esq.contains(b)) {
  141.                                 esq.add(b);
  142.                             }
  143.                         }
  144.                     }
  145.                 }
  146.             }
  147.             permCores = dir;
  148.             permBebidas = esq;
  149.  
  150.             //5. O Ucraniano bebe chá.
  151.             dir = new ArrayList<>();
  152.             esq = new ArrayList<>();
  153.             for (Object[] n : permNacionalidades) {
  154.                 for (Object[] b : permBebidas) {
  155.                     for (int i = 0; i < b.length; i++) {
  156.                         if (n[i] == (Object) Nacionalidade.UCRANIANO && b[i] == (Object) Bebida.CHA) {
  157.                             if (!dir.contains(b)) {
  158.                                 dir.add(b);
  159.                             }
  160.                             if (!esq.contains(n)) {
  161.                                 esq.add(n);
  162.                             }
  163.                         }
  164.                     }
  165.                 }
  166.  
  167.             }
  168.             permBebidas = dir;
  169.             permNacionalidades = esq;
  170.  
  171.             //6. A casa verde está imediatamente à direita da casa de marfim.
  172.             dir = new ArrayList<>();
  173.             for (Object[] c : permCores) {   //Verde
  174.                 for (int i = 0; i < c.length; i++) {
  175.                     if (i + 1 < c.length && c[i] == (Object) Cor.MARFIM && c[i + 1] == (Object) Cor.VERDE) {
  176.                         if (!dir.contains(c)) {
  177.                             dir.add(c);
  178.                             Arrays.deepToString(c);
  179.                         }
  180.                     }
  181.                 }
  182.             }
  183.             permCores = dir;
  184.  
  185.             //7. O fumador de Old Gold é dono de caracóis.
  186.             dir = new ArrayList<>();
  187.             esq = new ArrayList<>();
  188.             for (Object[] c : permCigarros) {
  189.                 for (Object[] a : permAnimais) {
  190.                     for (int i = 0; i < a.length; i++) {
  191.                         if (c[i] == (Object) Cigarro.OLDGOLD && a[i] == (Object) Animal.CARACOIS) {
  192.                             if (!dir.contains(a)) {
  193.                                 dir.add(a);
  194.                             }
  195.                             if (!esq.contains(c)) {
  196.                                 esq.add(c);
  197.                             }
  198.                         }
  199.                     }
  200.                 }
  201.  
  202.             }
  203.             permAnimais = dir;
  204.             permCigarros = esq;
  205.  
  206.             //8. Kools são fumados na casa amarela.
  207.             dir = new ArrayList<>();
  208.             esq = new ArrayList<>();
  209.             for (Object[] c : permCigarros) {
  210.                 for (Object[] cor : permCores) {
  211.                     for (int i = 0; i < cor.length; i++) {
  212.                         if (c[i] == (Object) Cigarro.KOOLS && cor[i] == (Object) Cor.AMARELA) {
  213.                             if (!dir.contains(cor)) {
  214.                                 dir.add(cor);
  215.                             }
  216.                             if (!esq.contains(c)) {
  217.                                 esq.add(c);
  218.                             }
  219.                         }
  220.                     }
  221.                 }
  222.  
  223.             }
  224.             permCores = dir;
  225.             permCigarros = esq;
  226.  
  227.             //9. Leite é bebido na casa do meio.
  228.             dir = new ArrayList<>();
  229.             for (Object[] b : permBebidas) {
  230.                 if (b[b.length / 2] == (Object) Bebida.LEITE) {
  231.                     dir.add(b);
  232.                 }
  233.             }
  234.             permBebidas = dir;
  235.  
  236.             //10. O Norueguês vive na primeira casa.
  237.             dir = new ArrayList<>();
  238.             for (Object[] n : permNacionalidades) {
  239.                 if (n[0] == (Object) Nacionalidade.NORUEGUES) {
  240.                     dir.add(n);
  241.                 }
  242.             }
  243.             permNacionalidades = dir;
  244.  
  245.             //11. O homem que fuma Chesterfields vive na casa ao lado do homem com a raposa.
  246.             dir = new ArrayList<>();
  247.             esq = new ArrayList<>();
  248.             for (Object[] c : permCigarros) {
  249.                 for (Object[] a : permAnimais) {
  250.                     for (int i = 0; i < a.length; i++) {
  251.                         if (i - 1 >= 0 && c[i - 1] == (Object) Cigarro.CHESTERFIELDS && a[i] == (Object) Animal.RAPOSA) {
  252.                             if (!dir.contains(a)) {
  253.                                 dir.add(a);
  254.                             }
  255.                             if (!esq.contains(c)) {
  256.                                 esq.add(c);
  257.                             }
  258.                         } else if (i + 1 < a.length && c[i + 1] == (Object) Cigarro.CHESTERFIELDS && a[i] == (Object) Animal.RAPOSA) {
  259.                             if (!dir.contains(a)) {
  260.                                 dir.add(a);
  261.                             }
  262.                             if (!esq.contains(c)) {
  263.                                 esq.add(c);
  264.                             }
  265.                         }
  266.                     }
  267.                 }
  268.  
  269.             }
  270.             permAnimais = dir;
  271.             permCigarros = esq;
  272.  
  273.             //12. Kools são fumados na casa ao lado daquela onde o cavalo é guardado.
  274.             dir = new ArrayList<>();
  275.             esq = new ArrayList<>();
  276.             for (Object[] c : permCigarros) {
  277.                 for (Object[] a : permAnimais) {
  278.                     for (int i = 0; i < a.length; i++) {
  279.                         if (i - 1 >= 0 && c[i - 1] == (Object) Cigarro.KOOLS && a[i] == (Object) Animal.CAVALO) {
  280.                             if (!dir.contains(a)) {
  281.                                 dir.add(a);
  282.                             }
  283.                             if (!esq.contains(c)) {
  284.                                 esq.add(c);
  285.                             }
  286.                         } else if (i + 1 < a.length && c[i + 1] == (Object) Cigarro.KOOLS && a[i] == (Object) Animal.CAVALO) {
  287.                             if (!dir.contains(a)) {
  288.                                 dir.add(a);
  289.                             }
  290.                             if (!esq.contains(c)) {
  291.                                 esq.add(c);
  292.                             }
  293.                         }
  294.                     }
  295.                 }
  296.  
  297.             }
  298.             permAnimais = dir;
  299.             permCigarros = esq;
  300.  
  301.             //13. O fumador de Lucky Strike bebe sumo de laranja.
  302.             dir = new ArrayList<>();
  303.             esq = new ArrayList<>();
  304.             for (Object[] c : permCigarros) {
  305.                 for (Object[] b : permBebidas) {
  306.                     for (int i = 0; i < b.length; i++) {
  307.                         if (c[i] == (Object) Cigarro.LUCKYSTRIKE && b[i] == (Object) Bebida.SUMO) {
  308.                             if (!dir.contains(b)) {
  309.                                 dir.add(b);
  310.                             }
  311.                             if (!esq.contains(c)) {
  312.                                 esq.add(c);
  313.                             }
  314.                         }
  315.                     }
  316.                 }
  317.  
  318.             }
  319.             permBebidas = dir;
  320.             permCigarros = esq;
  321.  
  322.             //14. O Japonês fuma Parliaments.
  323.             dir = new ArrayList<>();
  324.             esq = new ArrayList<>();
  325.             for (Object[] n : permNacionalidades) {
  326.                 for (Object[] c : permCigarros) {
  327.                     for (int i = 0; i < c.length; i++) {
  328.                         if (n[i] == (Object) Nacionalidade.JAPONES && c[i] == (Object) Cigarro.PARLIAMENTS) {
  329.                             if (!dir.contains(c)) {
  330.                                 dir.add(c);
  331.                             }
  332.                             if (!esq.contains(n)) {
  333.                                 esq.add(n);
  334.                             }
  335.                         }
  336.                     }
  337.                 }
  338.  
  339.             }
  340.             permCigarros = dir;
  341.             permNacionalidades = esq;
  342.  
  343.             //15. O Norueguês vive ao lado da casa azul.
  344.             dir = new ArrayList<>();
  345.             esq = new ArrayList<>();
  346.             for (Object[] n : permNacionalidades) {
  347.                 for (Object[] c : permCores) {
  348.                     for (int i = 0; i < c.length; i++) {
  349.                         if (i - 1 >= 0 && n[i - 1] == (Object) Nacionalidade.NORUEGUES && c[i] == (Object) Cor.AZUL) {
  350.                             if (!dir.contains(c)) {
  351.                                 dir.add(c);
  352.                             }
  353.                             if (!esq.contains(n)) {
  354.                                 esq.add(n);
  355.                             }
  356.                         } else if (i + 1 < n.length && n[i + 1] == (Object) Nacionalidade.NORUEGUES && c[i] == (Object) Cor.AZUL) {
  357.                             if (!dir.contains(c)) {
  358.                                 dir.add(c);
  359.                             }
  360.                             if (!esq.contains(n)) {
  361.                                 esq.add(n);
  362.                             }
  363.                         }
  364.                     }
  365.                 }
  366.  
  367.             }
  368.             permCores = dir;
  369.             permNacionalidades = esq;
  370.  
  371.             //Conta o numero total de permutações existentes
  372.             perms = permCores.size() + permNacionalidades.size() + permAnimais.size()
  373.                     + permBebidas.size() + permCigarros.size();
  374.  
  375.  
  376.             ciclo++; //Para evitar o ciclo infinito se não encontrar solução
  377.  
  378.             //Batota :)
  379.             if (permCores.size() == 2) {
  380.                 permCores.remove(1);
  381.             }
  382.             if (permBebidas.size() == 2) {
  383.                 permBebidas.remove(1);
  384.             }
  385.         }
  386.         if (perms == 5) {
  387.             System.out.format("%8s%14s%14s%14s%14s%n", "_", "_", "_", "_", "_");
  388.             System.out.format("%9s%14s%14s%14s%14s%n", "/ \\","/ \\","/ \\","/ \\","/ \\");
  389.             System.out.format("%6s%4s%10s%4s%10s%4s%10s%4s%10s%4s%n", "/","\\", "/","\\", "/","\\", "/","\\", "/","\\");
  390.             System.out.format("%5s%6s%8s%6s%8s%6s%8s%6s%8s%6s%n", "/","\\", "/","\\", "/","\\", "/","\\", "/","\\");
  391.             System.out.format("%4s%8s%6s%8s%6s%8s%6s%8s%6s%8s%n", "/","\\", "/","\\", "/","\\", "/","\\", "/","\\");
  392.             System.out.format("%3s%5s%5s%4s%5s%5s%4s%5s%5s%4s%5s%5s%4s%5s%5s%n", "/","1","\\", "/","2","\\", "/","3","\\", "/","4","\\", "/","5","\\");
  393.             System.out.format("%2s%12s%2s%12s%2s%12s%2s%12s%2s%12s%n", "/","\\", "/","\\", "/","\\", "/","\\", "/","\\");
  394.            
  395.             for(int i=0;i<71;i++){
  396.                 System.out.print("-");
  397.             }
  398.             System.out.print("\n|");
  399.             for (Object c : permCores.get(0)) {
  400.                 System.out.format("%13s|", c);
  401.             }
  402.             System.out.print("\n|");//1(i0) e 5(i4) negrito
  403.             for (Object n : permNacionalidades.get(0)) {
  404.                 System.out.format("%13s|", n);
  405.             }
  406.             System.out.print("\n|");//5(i4) negrito
  407.             for (Object a : permAnimais.get(0)) {
  408.                 System.out.format("%13s|", a);
  409.             }
  410.             System.out.print("\n|");//1(i0) negrito
  411.             for (Object b : permBebidas.get(0)) {
  412.                 System.out.format("%13s|", b);
  413.             }
  414.             System.out.print("\n|");
  415.             for (Object c : permCigarros.get(0)) {
  416.                 System.out.format("%13s|", c);
  417.             }
  418.             System.out.print("");
  419.             System.out.println();
  420.             for(int i=0;i<71;i++){
  421.                 System.out.print("-");
  422.             }
  423.             System.out.print("\n");
  424.  
  425.             int agua = 0;
  426.             int zebra = 0;
  427.  
  428.             for (int i = 0; i < 5; i++) {
  429.                 if (permBebidas.get(0)[i] == Bebida.AGUA) {
  430.                     agua = i;
  431.                 }
  432.                 if (permAnimais.get(0)[i] == Animal.ZEBRA) {
  433.                     zebra = i;
  434.                 }
  435.             }
  436.  
  437.             System.out.println("Quem bebe água?");
  438.             System.out.println(permNacionalidades.get(0)[agua]);
  439.             System.out.println("Quem é o dono da zebra?");
  440.             System.out.println(permNacionalidades.get(0)[zebra]);
  441.         }
  442.     }
  443. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement