Avatar of crane-yuan

crane-yuan's solution

to Zebra Puzzle in the Java Track

Published at Aug 17 2019 · 0 comments
Instructions
Test suite
Solution

Solve the zebra puzzle.

  1. There are five houses.
  2. The Englishman lives in the red house.
  3. The Spaniard owns the dog.
  4. Coffee is drunk in the green house.
  5. The Ukrainian drinks tea.
  6. The green house is immediately to the right of the ivory house.
  7. The Old Gold smoker owns snails.
  8. Kools are smoked in the yellow house.
  9. Milk is drunk in the middle house.
  10. The Norwegian lives in the first house.
  11. The man who smokes Chesterfields lives in the house next to the man with the fox.
  12. Kools are smoked in the house next to the house where the horse is kept.
  13. The Lucky Strike smoker drinks orange juice.
  14. The Japanese smokes Parliaments.
  15. The Norwegian lives next to the blue house.

Each of the five houses is painted a different color, and their inhabitants are of different national extractions, own different pets, drink different beverages and smoke different brands of cigarettes.

Which of the residents drinks water? Who owns the zebra?

Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

$ gradle test

Use gradlew.bat if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore("Remove to run test") annotation.

Source

Wikipedia https://en.wikipedia.org/wiki/Zebra_Puzzle

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

ZebraPuzzleTest.java

import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class ZebraPuzzleTest {

    @Test
    public void residentWhoDrinksWater() {
        ZebraPuzzle zebraPuzzle = new ZebraPuzzle();
        assertEquals("Norwegian", zebraPuzzle.getWaterDrinker());
    }

    @Ignore("Remove to run test")
    @Test
    public void residentWhoOwnsZebra() {
        ZebraPuzzle zebraPuzzle = new ZebraPuzzle();
        assertEquals("Japanese", zebraPuzzle.getZebraOwner());
    }

}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ZebraPuzzle {
    private static List<Color> colorSolution;
    private static List<Nationality> nationalitySolution;
    private static List<Drink> drinkSolution;
    private static List<Smoke> smokeSolution;
    private static List<Pet> petSolution;

    static {
        init();
    }

    private static void init() {
        List<List<Color>> colorPermutations = buildColorPermutations();
        List<List<Nationality>> nationalityPermutations = buildNationalityPermutations();
        List<List<Drink>> drinkPermutations = buildDrinkPermutations();
        List<List<Smoke>> smokePermutations = buildSmokePermutations();
        List<List<Pet>> petPermutations = buildPetPermutations();

        for (List<Color> colorPermutation : colorPermutations) {
            for (List<Nationality> nationalityPermutation : nationalityPermutations) {
                for (List<Drink> drinkPermutation : drinkPermutations) {
                    for (List<Smoke> smokePermutation : smokePermutations) {
                        for (List<Pet> petPermutation : petPermutations) {
                            if (check(colorPermutation, nationalityPermutation, drinkPermutation, smokePermutation, petPermutation)) {
                                colorSolution = colorPermutation;
                                nationalitySolution = nationalityPermutation;
                                drinkSolution = drinkPermutation;
                                smokeSolution = smokePermutation;
                                petSolution = petPermutation;

                                return;
                            }
                        }
                    }
                }
            }
        }
    }

    private static boolean check(List<Color> colorPermutation, List<Nationality> nationalityPermutation, List<Drink> drinkPermutation,
                                 List<Smoke> smokePermutation, List<Pet> petPermutation) {
        return nationalityPermutation.indexOf(Nationality.ENGLISHMAN) == colorPermutation.indexOf(Color.RED)
                && nationalityPermutation.indexOf(Nationality.SPANIARD) == petPermutation.indexOf(Pet.DOG)
                && drinkPermutation.indexOf(Drink.COFFEE) == colorPermutation.indexOf(Color.GREEN)
                && nationalityPermutation.indexOf(Nationality.UKRAINIAN) == drinkPermutation.indexOf(Drink.TEA)
                && colorPermutation.indexOf(Color.GREEN) == colorPermutation.indexOf(Color.IVORY) + 1
                && smokePermutation.indexOf(Smoke.OLD_GOLD) == petPermutation.indexOf(Pet.SNAILS)
                && smokePermutation.indexOf(Smoke.KOOLS) == colorPermutation.indexOf(Color.YELLOW)
                && Math.abs(smokePermutation.indexOf(Smoke.CHESTERFIELDS) - petPermutation.indexOf(Pet.FOX)) == 1
                && Math.abs(smokePermutation.indexOf(Smoke.KOOLS) - petPermutation.indexOf(Pet.HORSE)) == 1
                && smokePermutation.indexOf(Smoke.LUCKY_STRIKE) == drinkPermutation.indexOf(Drink.ORANGE_JUICE)
                && nationalityPermutation.indexOf(Nationality.JAPANESE) == smokePermutation.indexOf(Smoke.PARLIAMENTS);
    }

    private static List<List<Color>> buildColorPermutations() {
        return generatePermutations(Color.class).stream()
                                                .filter(permutation -> permutation.get(1) == Color.BLUE)
                                                .collect(Collectors.toList());
    }

    private static List<List<Nationality>> buildNationalityPermutations() {
        return generatePermutations(Nationality.class).stream()
                                                      .filter(permutation -> permutation.get(0) == Nationality.NORWEGIAN)
                                                      .collect(Collectors.toList());
    }

    private static List<List<Drink>> buildDrinkPermutations() {
        return generatePermutations(Drink.class).stream()
                                                .filter(permutation -> permutation.get(2) == Drink.MILK)
                                                .collect(Collectors.toList());
    }

    private static List<List<Smoke>> buildSmokePermutations() {
        return generatePermutations(Smoke.class);
    }

    private static List<List<Pet>> buildPetPermutations() {
        return generatePermutations(Pet.class);
    }

    private static <T> List<List<T>> generatePermutations(Class<T> enumClass) {
        T[] values = enumClass.getEnumConstants();

        List<List<T>> permutations = new ArrayList<>();
        search(permutations, values, 0);
        return permutations;
    }

    private static <T> void search(List<List<T>> permutations, T[] values, int index) {
        if (index == values.length) {
            permutations.add(new ArrayList<>(Arrays.asList(values)));

            return;
        }

        for (int i = index; i < values.length; i++) {
            swap(values, index, i);
            search(permutations, values, index + 1);
            swap(values, index, i);
        }
    }

    private static <T> void swap(T[] values, int index1, int index2) {
        T temp = values[index1];
        values[index1] = values[index2];
        values[index2] = temp;
    }

    String getWaterDrinker() {
        return nationalitySolution.get(drinkSolution.indexOf(Drink.WATER)).name;
    }

    String getZebraOwner() {
        return nationalitySolution.get(petSolution.indexOf(Pet.ZEBRA)).name;
    }
}


enum Color {
    RED,
    GREEN,
    IVORY,
    YELLOW,
    BLUE
}


enum Nationality {
    ENGLISHMAN("Englishman"),
    SPANIARD("Spaniard"),
    UKRAINIAN("Ukrainian"),
    NORWEGIAN("Norwegian"),
    JAPANESE("Japanese");

    String name;

    Nationality(String name) {
        this.name = name;
    }
}


enum Drink {
    COFFEE,
    TEA,
    MILK,
    ORANGE_JUICE,
    WATER
}


enum Smoke {
    OLD_GOLD,
    KOOLS,
    CHESTERFIELDS,
    LUCKY_STRIKE,
    PARLIAMENTS
}


enum Pet {
    DOG,
    SNAILS,
    FOX,
    HORSE,
    ZEBRA
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?