Avatar of VicenteFreire

VicenteFreire's solution

to Zebra Puzzle in the Java Track

Published at Apr 23 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;

public class ZebraPuzzle {

    private enum Owner { NORWEGIAN, UKRAINIAN, SPANIARD, ENGLISHMAN, JAPANESE }
    private enum Color { BLUE, RED, YELLOW, GREEN, IVORY }
    private enum Beverage { COFFEE, TEA, ORANGE_JUICE, WATER, MILK }
    private enum Pet { DOG, SNAILS, HORSE, FOX, ZEBRA }
    private enum Cigarettes { KOOLS, LUCKY_STRIKE, CHESTERFIELD, PARLIAMENTS, OLD_GOLD }

    private static class Solution {
        List<Owner> owners;
        List<Color> colors;
        List<Beverage> beverages;
        List<Pet> pets;
        List<Cigarettes> cigarettes;
    }

    private Solution solution;

    ZebraPuzzle() {
        this.solution = generateSolution();
    }

    private Solution generateSolution() {
        Solution houses = new Solution();

        outer:
        for (List<Owner> owners : getPermutations(Owner.values())) {
            if (owners.indexOf(Owner.NORWEGIAN) != 0) {
                continue;
            }

            for (List<Color> colors : getPermutations(Color.values())) {
                if ((colors.indexOf(Color.BLUE) != 1)
                    || (colors.indexOf(Color.RED) != owners.indexOf(Owner.ENGLISHMAN))
                    || (!isJustAfter(colors, Color.GREEN, colors, Color.IVORY))) {
                    continue;
                }

                for (List<Beverage> beverages : getPermutations(Beverage.values())) {
                    if ((beverages.indexOf(Beverage.TEA) != owners.indexOf(Owner.UKRAINIAN))
                        || (beverages.indexOf(Beverage.MILK) != 2)
                        || (beverages.indexOf(Beverage.COFFEE) != colors.indexOf(Color.GREEN))){
                        continue;
                    }

                    for (List<Pet> pets : getPermutations(Pet.values())) {
                        if (pets.indexOf(Pet.DOG) != owners.indexOf(Owner.SPANIARD)) {
                            continue;
                        }

                        for (List<Cigarettes> cigarettes : getPermutations(Cigarettes.values())) {
                            if ((cigarettes.indexOf(Cigarettes.OLD_GOLD) != pets.indexOf(Pet.SNAILS))
                                || (cigarettes.indexOf(Cigarettes.KOOLS) != colors.indexOf(Color.YELLOW))
                                || (cigarettes.indexOf(Cigarettes.PARLIAMENTS) != owners.indexOf(Owner.JAPANESE))
                                || (!isNext(cigarettes, Cigarettes.CHESTERFIELD, pets, Pet.FOX))
                                || (!isNext(cigarettes, Cigarettes.KOOLS, pets, Pet.HORSE))
                                || (cigarettes.indexOf(Cigarettes.LUCKY_STRIKE)
                                    != beverages.indexOf(Beverage.ORANGE_JUICE))) {
                                continue;
                            }

                            houses.owners = owners;
                            houses.colors = colors;
                            houses.beverages = beverages;
                            houses.pets = pets;
                            houses.cigarettes = cigarettes;

                            break outer;
                        }
                    }
                }
            }
        }

        return houses;
    }

    private static <T> List<List<T>> getPermutations(T[] array) {
        List<List<T>> permutations = new ArrayList<>();
        permute(permutations, array, 0);

        return permutations;
    }

    private static <T> void permute(List<List<T>> permutations, T[] array, int startIndex) {
        if (startIndex == (array.length - 1)) {
            permutations.add(new ArrayList<>(Arrays.asList(array)));
        }

        for (int i = startIndex; i < array.length; i++) {
            swap(array, startIndex, i);
            permute(permutations, array, startIndex + 1);
            swap(array, startIndex, i);
        }
    }

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

    private static <T, U> boolean isNext(List<T> list1, T element1, List<U> list2, U element2) {
        return isJustBefore(list1, element1, list2, element2)
                || isJustAfter(list1, element1, list2, element2);
    }

    private static <T, U> boolean isJustBefore(List<T> list1, T element1, List<U> list2, U element2) {
        return (list1.indexOf(element1) < (list1.size() - 1))
                && (list2.get(list1.indexOf(element1) + 1) == element2);
    }

    private static <T, U> boolean isJustAfter(List<T> list1, T element1, List<U> list2, U element2) {
        return (list1.indexOf(element1) > 0)
                && (list2.get(list1.indexOf(element1) - 1) == element2);
    }

    private static <T, U> T get(List<T> searchList, List<U> filterList, U element) {
        return searchList.get(filterList.indexOf(element));
    }

    public String getWaterDrinker() {
        return capitalizeFirst(get(this.solution.owners, this.solution.beverages, Beverage.WATER).name());
    }

    public String getZebraOwner() {
        return capitalizeFirst(get(this.solution.owners, this.solution.pets, Pet.ZEBRA).name());
    }

    private static String capitalizeFirst(String value) {
        return value.substring(0, 1).toUpperCase() + value.substring(1).toLowerCase();
    }

}

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?