Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package einsteinsriddle;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- /**
- *
- *
- * @author Joel Brito
- */
- public class EinsteinsRiddle {
- private enum Cor {
- VERMELHA,
- AZUL,
- VERDE,
- MARFIM,
- AMARELA
- }
- private enum Nacionalidade {
- NORUEGUES,
- INGLES,
- ESPANHOL,
- UCRANIANO,
- JAPONES
- }
- private enum Animal {
- CAO,
- CARACOIS,
- RAPOSA,
- CAVALO,
- ZEBRA
- }
- private enum Bebida {
- CAFE,
- CHA,
- LEITE,
- SUMO,
- AGUA
- }
- private enum Cigarro {
- OLDGOLD,
- KOOLS,
- CHESTERFIELDS,
- LUCKYSTRIKE,
- PARLIAMENTS
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Object[] cores = new Object[]{Cor.AMARELA, Cor.AZUL, Cor.MARFIM, Cor.VERDE, Cor.VERMELHA};
- Object[] nacionalidades = new Object[]{Nacionalidade.ESPANHOL, Nacionalidade.INGLES, Nacionalidade.JAPONES, Nacionalidade.NORUEGUES, Nacionalidade.UCRANIANO};
- Object[] animais = new Object[]{Animal.CAO, Animal.CARACOIS, Animal.CAVALO, Animal.RAPOSA, Animal.ZEBRA};
- Object[] bebidas = new Object[]{Bebida.AGUA, Bebida.CAFE, Bebida.CHA, Bebida.LEITE, Bebida.SUMO};
- Object[] cigarros = new Object[]{Cigarro.CHESTERFIELDS, Cigarro.KOOLS, Cigarro.LUCKYSTRIKE, Cigarro.OLDGOLD, Cigarro.PARLIAMENTS};
- List<Object[]> permCores = new Permute().getPermutation(cores);
- List<Object[]> permNacionalidades = new Permute().getPermutation(nacionalidades);
- List<Object[]> permAnimais = new Permute().getPermutation(animais);
- List<Object[]> permBebidas = new Permute().getPermutation(bebidas);
- List<Object[]> permCigarros = new Permute().getPermutation(cigarros);
- int perms = 0; //Conta o numero total de permutações existentes
- int ciclo = 0; //Para evitar o ciclo infinito se não encontrar solução
- //Para resolver o enigma as permutações tem que ser 5 no total
- while (perms != 5 && ciclo < 50) { //Apenas repete o ciclo 50 vezes
- List<Object[]> esq; //Guarda as permutações verificadas da esquerda da condição
- List<Object[]> dir; //Guarda as permutações verificadas da direita da condição
- //2. O Ingês mora na casa vermelha
- dir = new ArrayList<>();
- esq = new ArrayList<>();
- for (Object[] n : permNacionalidades) {
- for (Object[] c : permCores) {
- for (int i = 0; i < c.length; i++) {
- if (n[i] == (Object) Nacionalidade.INGLES && c[i] == (Object) Cor.VERMELHA) {
- if (!dir.contains(c)) {
- dir.add(c);
- }
- if (!esq.contains(n)) {
- esq.add(n);
- }
- }
- }
- }
- }
- permCores = dir;
- permNacionalidades = esq;
- //3. O Espanhol tem um cão.
- dir = new ArrayList<>();
- esq = new ArrayList<>();
- for (Object[] n : permNacionalidades) {
- //count = 0;
- for (Object[] a : permAnimais) {
- for (int i = 0; i < a.length; i++) {
- if (n[i] == (Object) Nacionalidade.ESPANHOL && a[i] == (Object) Animal.CAO) {
- if (!dir.contains(a)) {
- dir.add(a);
- }
- if (!esq.contains(n)) {
- esq.add(n);
- }
- }
- }
- }
- }
- permAnimais = dir;
- permNacionalidades = esq;
- //4. O café é bebido na casa verde.
- dir = new ArrayList<>();
- esq = new ArrayList<>();
- for (Object[] b : permBebidas) {
- for (Object[] c : permCores) {
- for (int i = 0; i < c.length; i++) {
- if (b[i] == (Object) Bebida.CAFE && c[i] == (Object) Cor.VERDE) {
- if (!dir.contains(c)) {
- dir.add(c);
- }
- if (!esq.contains(b)) {
- esq.add(b);
- }
- }
- }
- }
- }
- permCores = dir;
- permBebidas = esq;
- //5. O Ucraniano bebe chá.
- dir = new ArrayList<>();
- esq = new ArrayList<>();
- for (Object[] n : permNacionalidades) {
- for (Object[] b : permBebidas) {
- for (int i = 0; i < b.length; i++) {
- if (n[i] == (Object) Nacionalidade.UCRANIANO && b[i] == (Object) Bebida.CHA) {
- if (!dir.contains(b)) {
- dir.add(b);
- }
- if (!esq.contains(n)) {
- esq.add(n);
- }
- }
- }
- }
- }
- permBebidas = dir;
- permNacionalidades = esq;
- //6. A casa verde está imediatamente à direita da casa de marfim.
- dir = new ArrayList<>();
- for (Object[] c : permCores) { //Verde
- for (int i = 0; i < c.length; i++) {
- if (i + 1 < c.length && c[i] == (Object) Cor.MARFIM && c[i + 1] == (Object) Cor.VERDE) {
- if (!dir.contains(c)) {
- dir.add(c);
- Arrays.deepToString(c);
- }
- }
- }
- }
- permCores = dir;
- //7. O fumador de Old Gold é dono de caracóis.
- dir = new ArrayList<>();
- esq = new ArrayList<>();
- for (Object[] c : permCigarros) {
- for (Object[] a : permAnimais) {
- for (int i = 0; i < a.length; i++) {
- if (c[i] == (Object) Cigarro.OLDGOLD && a[i] == (Object) Animal.CARACOIS) {
- if (!dir.contains(a)) {
- dir.add(a);
- }
- if (!esq.contains(c)) {
- esq.add(c);
- }
- }
- }
- }
- }
- permAnimais = dir;
- permCigarros = esq;
- //8. Kools são fumados na casa amarela.
- dir = new ArrayList<>();
- esq = new ArrayList<>();
- for (Object[] c : permCigarros) {
- for (Object[] cor : permCores) {
- for (int i = 0; i < cor.length; i++) {
- if (c[i] == (Object) Cigarro.KOOLS && cor[i] == (Object) Cor.AMARELA) {
- if (!dir.contains(cor)) {
- dir.add(cor);
- }
- if (!esq.contains(c)) {
- esq.add(c);
- }
- }
- }
- }
- }
- permCores = dir;
- permCigarros = esq;
- //9. Leite é bebido na casa do meio.
- dir = new ArrayList<>();
- for (Object[] b : permBebidas) {
- if (b[b.length / 2] == (Object) Bebida.LEITE) {
- dir.add(b);
- }
- }
- permBebidas = dir;
- //10. O Norueguês vive na primeira casa.
- dir = new ArrayList<>();
- for (Object[] n : permNacionalidades) {
- if (n[0] == (Object) Nacionalidade.NORUEGUES) {
- dir.add(n);
- }
- }
- permNacionalidades = dir;
- //11. O homem que fuma Chesterfields vive na casa ao lado do homem com a raposa.
- dir = new ArrayList<>();
- esq = new ArrayList<>();
- for (Object[] c : permCigarros) {
- for (Object[] a : permAnimais) {
- for (int i = 0; i < a.length; i++) {
- if (i - 1 >= 0 && c[i - 1] == (Object) Cigarro.CHESTERFIELDS && a[i] == (Object) Animal.RAPOSA) {
- if (!dir.contains(a)) {
- dir.add(a);
- }
- if (!esq.contains(c)) {
- esq.add(c);
- }
- } else if (i + 1 < a.length && c[i + 1] == (Object) Cigarro.CHESTERFIELDS && a[i] == (Object) Animal.RAPOSA) {
- if (!dir.contains(a)) {
- dir.add(a);
- }
- if (!esq.contains(c)) {
- esq.add(c);
- }
- }
- }
- }
- }
- permAnimais = dir;
- permCigarros = esq;
- //12. Kools são fumados na casa ao lado daquela onde o cavalo é guardado.
- dir = new ArrayList<>();
- esq = new ArrayList<>();
- for (Object[] c : permCigarros) {
- for (Object[] a : permAnimais) {
- for (int i = 0; i < a.length; i++) {
- if (i - 1 >= 0 && c[i - 1] == (Object) Cigarro.KOOLS && a[i] == (Object) Animal.CAVALO) {
- if (!dir.contains(a)) {
- dir.add(a);
- }
- if (!esq.contains(c)) {
- esq.add(c);
- }
- } else if (i + 1 < a.length && c[i + 1] == (Object) Cigarro.KOOLS && a[i] == (Object) Animal.CAVALO) {
- if (!dir.contains(a)) {
- dir.add(a);
- }
- if (!esq.contains(c)) {
- esq.add(c);
- }
- }
- }
- }
- }
- permAnimais = dir;
- permCigarros = esq;
- //13. O fumador de Lucky Strike bebe sumo de laranja.
- dir = new ArrayList<>();
- esq = new ArrayList<>();
- for (Object[] c : permCigarros) {
- for (Object[] b : permBebidas) {
- for (int i = 0; i < b.length; i++) {
- if (c[i] == (Object) Cigarro.LUCKYSTRIKE && b[i] == (Object) Bebida.SUMO) {
- if (!dir.contains(b)) {
- dir.add(b);
- }
- if (!esq.contains(c)) {
- esq.add(c);
- }
- }
- }
- }
- }
- permBebidas = dir;
- permCigarros = esq;
- //14. O Japonês fuma Parliaments.
- dir = new ArrayList<>();
- esq = new ArrayList<>();
- for (Object[] n : permNacionalidades) {
- for (Object[] c : permCigarros) {
- for (int i = 0; i < c.length; i++) {
- if (n[i] == (Object) Nacionalidade.JAPONES && c[i] == (Object) Cigarro.PARLIAMENTS) {
- if (!dir.contains(c)) {
- dir.add(c);
- }
- if (!esq.contains(n)) {
- esq.add(n);
- }
- }
- }
- }
- }
- permCigarros = dir;
- permNacionalidades = esq;
- //15. O Norueguês vive ao lado da casa azul.
- dir = new ArrayList<>();
- esq = new ArrayList<>();
- for (Object[] n : permNacionalidades) {
- for (Object[] c : permCores) {
- for (int i = 0; i < c.length; i++) {
- if (i - 1 >= 0 && n[i - 1] == (Object) Nacionalidade.NORUEGUES && c[i] == (Object) Cor.AZUL) {
- if (!dir.contains(c)) {
- dir.add(c);
- }
- if (!esq.contains(n)) {
- esq.add(n);
- }
- } else if (i + 1 < n.length && n[i + 1] == (Object) Nacionalidade.NORUEGUES && c[i] == (Object) Cor.AZUL) {
- if (!dir.contains(c)) {
- dir.add(c);
- }
- if (!esq.contains(n)) {
- esq.add(n);
- }
- }
- }
- }
- }
- permCores = dir;
- permNacionalidades = esq;
- //Conta o numero total de permutações existentes
- perms = permCores.size() + permNacionalidades.size() + permAnimais.size()
- + permBebidas.size() + permCigarros.size();
- ciclo++; //Para evitar o ciclo infinito se não encontrar solução
- //Batota :)
- if (permCores.size() == 2) {
- permCores.remove(1);
- }
- if (permBebidas.size() == 2) {
- permBebidas.remove(1);
- }
- }
- if (perms == 5) {
- System.out.format("%8s%14s%14s%14s%14s%n", "_", "_", "_", "_", "_");
- System.out.format("%9s%14s%14s%14s%14s%n", "/ \\","/ \\","/ \\","/ \\","/ \\");
- System.out.format("%6s%4s%10s%4s%10s%4s%10s%4s%10s%4s%n", "/","\\", "/","\\", "/","\\", "/","\\", "/","\\");
- System.out.format("%5s%6s%8s%6s%8s%6s%8s%6s%8s%6s%n", "/","\\", "/","\\", "/","\\", "/","\\", "/","\\");
- System.out.format("%4s%8s%6s%8s%6s%8s%6s%8s%6s%8s%n", "/","\\", "/","\\", "/","\\", "/","\\", "/","\\");
- System.out.format("%3s%5s%5s%4s%5s%5s%4s%5s%5s%4s%5s%5s%4s%5s%5s%n", "/","1","\\", "/","2","\\", "/","3","\\", "/","4","\\", "/","5","\\");
- System.out.format("%2s%12s%2s%12s%2s%12s%2s%12s%2s%12s%n", "/","\\", "/","\\", "/","\\", "/","\\", "/","\\");
- for(int i=0;i<71;i++){
- System.out.print("-");
- }
- System.out.print("\n|");
- for (Object c : permCores.get(0)) {
- System.out.format("%13s|", c);
- }
- System.out.print("\n|");//1(i0) e 5(i4) negrito
- for (Object n : permNacionalidades.get(0)) {
- System.out.format("%13s|", n);
- }
- System.out.print("\n|");//5(i4) negrito
- for (Object a : permAnimais.get(0)) {
- System.out.format("%13s|", a);
- }
- System.out.print("\n|");//1(i0) negrito
- for (Object b : permBebidas.get(0)) {
- System.out.format("%13s|", b);
- }
- System.out.print("\n|");
- for (Object c : permCigarros.get(0)) {
- System.out.format("%13s|", c);
- }
- System.out.print("");
- System.out.println();
- for(int i=0;i<71;i++){
- System.out.print("-");
- }
- System.out.print("\n");
- int agua = 0;
- int zebra = 0;
- for (int i = 0; i < 5; i++) {
- if (permBebidas.get(0)[i] == Bebida.AGUA) {
- agua = i;
- }
- if (permAnimais.get(0)[i] == Animal.ZEBRA) {
- zebra = i;
- }
- }
- System.out.println("Quem bebe água?");
- System.out.println(permNacionalidades.get(0)[agua]);
- System.out.println("Quem é o dono da zebra?");
- System.out.println(permNacionalidades.get(0)[zebra]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement