class Fish {
    public static final int nofHouses     = 5;
    public static final int nofProperties = 5;
 
    public static final int NATIONALITY = 0;
    public static final int COLORS      = 1;
    public static final int CIGAR       = 2;
    public static final int PET         = 3;
    public static final int DRINK       = 4;

    static class State {
        final String[][] houses;

        State (String[][] houses) {
            this.houses = houses;
        }
    }

    public static void checkAndPrint(String[][] houses) throws Exception {

        String[][] houses1 = new String[nofHouses][nofProperties];
        for (int i=0; i<nofHouses; i++) {
            for (int j=0; j<nofProperties; j++) {
                houses1[i][j] = houses[i][j];
            }
        } 

        State s = new State (houses1);

        condition9(s);
        condition13(s);
        condition4(s);
        condition7(s);
        condition1(s);
        condition2(s);
        condition3(s);
        condition5(s);
        condition6(s);
        condition8(s);
        condition14(s);
        condition12(s);
        condition10(s);
        condition11(s);
        condition15(s);
 
        int iHouse = iHouseWithFish(s);

        System.out.println("The owner of a fish is " + owner(s, iHouse));

        for (int i=0; i<nofHouses; i++) {
            String out = "House " + (i+1) + ": ";
            for (int j=0; j<nofProperties; j++) {
                out+=(s.houses[i][j]+" ");
            }
            System.out.println(out);
        }
    }

    public static String owner(State s, int iHouse) {
        return s.houses[iHouse][NATIONALITY];
    }

    // 1. The Brit lives in a red house.

    public static void condition1(State s) throws Exception {
        inOneHouse(s, "Brit", "Red");
    }

    // 2. The Swede keeps dogs as pets.

    public static void condition2(State s) throws Exception {
        inOneHouse(s, "Swede", "Dog");
    }

    // 3. The Dane drinks tea.

    public static void condition3(State s) throws Exception {
        inOneHouse(s, "Dane", "Tea");
    }

    // 4. The green house is on the left of the white house.

    public static void condition4(State s) throws Exception {
        for (int i=0; i<nofHouses-1; i++) {
            if (s.houses[i]  [COLORS]=="Green") {
                s.houses[i]  [COLORS]= "Green";
            if (s.houses[i+1][COLORS]=="White") {
                s.houses[i+1][COLORS]= "White";
                return;
            }}
        }
        throw new Exception();
    }

    // 5. The green house owner drinks coffee.

    public static void condition5(State s) throws Exception {
        inOneHouse(s, "Green", "Coffee");
    }

    // 6. The person who smokes Pall Mall rears birds.

    public static void condition6(State s) throws Exception {
        inOneHouse(s, "PallMall", "Bird");
    }

    // 7. The man living in the house right in the centre drinks milk.

    public static void condition7(State s) throws Exception {
        if (s.houses[(nofHouses-1)/2][DRINK]=="Milk")
        {   s.houses[(nofHouses-1)/2][DRINK]= "Milk";
            return;
        }
        throw new Exception();
    }

    // 8. The owner of the yellow house smokes Dunhill.

    public static void condition8(State s) throws Exception {
        inOneHouse(s, "Yellow", "Dunhill");
    }

    // 9. The Norwegian lives in the first house.

    public static void condition9(State s) throws Exception {
        if (s.houses[0][NATIONALITY]=="Norwegian")
        {   s.houses[0][NATIONALITY]= "Norwegian";
            return;
        }
        throw new Exception();
    }

    // 10. The man who smokes Blend lives next to the one who keeps cats.

    public static void condition10(State s) throws Exception {
        inNextHouse(s, "Blend", "Cat");
    }

    // 11. The man who keeps horses lives next to the man who smokes Dunhill.

    public static void condition11(State s) throws Exception {
        inNextHouse(s, "Dunhill", "Horse");
    }

    // 12. The owner who smokes Blue Master drinks beer.

    public static void condition12(State s) throws Exception {
        inOneHouse(s, "BlueMaster", "Beer");
    }

    // 13. The Norwegian lives next to the blue house.

    public static void condition13(State s) throws Exception {
        inNextHouse(s, "Norwegian", "Blue");
    }

    // 14. The German smokes Prince.

    public static void condition14(State s) throws Exception {
        inOneHouse(s, "German", "Prince");
    }

    // 15. The man who smokes Blend has a neighbour who drinks water

    public static void condition15(State s) throws Exception {
        inNextHouse(s, "Blend", "Water");
    }

    // Find the number of the house with a fish

    public static int iHouseWithFish(State s) throws Exception {
        for (int i=0; i<nofHouses; i++) {
            if (s.houses[i][PET]=="Fish")
            {   s.houses[i][PET]= "Fish";
                return i;
            }
        }
        throw new Exception();
    }

    public static void inOneHouse(State s, String str1, String str2) throws Exception {
        for (int i=0; i<nofHouses; i++) {
            if (s.houses[i][index(str1)]==str1) {
                s.houses[i][index(str1)]= str1;
            if (s.houses[i][index(str2)]==str2) {
                s.houses[i][index(str2)]= str2;
                return;
            }}
        }
        throw new Exception();
    }

    public static void inNextHouse(State s, String str1, String str2) throws Exception {
        for (int i=0; i<nofHouses-1; i++) {
jscp_joinLabeled: {
            if (s.houses[i]  [index(str1)]==str1) {
                s.houses[i]  [index(str1)]= str1;
            if (s.houses[i+1][index(str2)]==str2) {
                s.houses[i+1][index(str2)]= str2;
                return;
            }}
}
            if (s.houses[i+1][index(str1)]==str1) {
                s.houses[i+1][index(str1)]= str1;
            if (s.houses[i]  [index(str2)]==str2) {
                s.houses[i]  [index(str2)]= str2;
                return;
            }}
        }
        throw new Exception();
    }

    public static int index(String str) throws Exception {
        if(str=="Norwegian" ||
           str=="Dane"      ||
           str=="Brit"      ||
           str=="German"    ||
           str=="Swede"      ) return NATIONALITY; 

        if(str=="Yellow"    ||
           str=="Blue"      ||
           str=="Red"       ||
           str=="Green"     || 
           str=="White"      ) return COLORS;

        if(str=="Dunhill"   ||
           str=="Blend"     ||
           str=="PallMall"  ||
           str=="Prince"    || 
           str=="BlueMaster" ) return CIGAR;

        if(str=="Cat"       ||
           str=="Horse"     ||
           str=="Bird"      || 
           str=="Fish"      || 
           str=="Dog"        ) return PET;

        if(str=="Water"     ||
           str=="Tea"       ||
           str=="Milk"      ||
           str=="Coffee"    ||
           str=="Beer"       ) return DRINK;

        throw new Exception();
    }

    public static void main (String args[]) throws Exception {
        String[][] houses = {
            {"Norwegian", "Yellow", "Dunhill",    "Cat",   "Water" },
            {"Dane",      "Blue",   "Blend",      "Horse", "Tea"   },
            {"Brit",      "Red",    "PallMall",   "Bird",  "Milk"  },
            {"German",    "Green",  "Prince",     "Fish",  "Coffee"},
            {"Swede",     "White",  "BlueMaster", "Dog",   "Beer"  }
        };
        checkAndPrint (houses);
    }
}