|
| 1 | +import java.util.Arrays; |
| 2 | +import java.util.HashMap; |
| 3 | +import java.util.Map; |
| 4 | +import java.util.stream.Stream; |
| 5 | + |
| 6 | +class ZebraPuzzle { |
| 7 | + private static final int FIRST = 1; |
| 8 | + private static final int MIDDLE = 3; |
| 9 | + |
| 10 | + private int red; |
| 11 | + private int green; |
| 12 | + private int ivory; |
| 13 | + private int yellow; |
| 14 | + private int blue; |
| 15 | + |
| 16 | + private int english; |
| 17 | + private int spaniard; |
| 18 | + private int ukrainian; |
| 19 | + private int japanese; |
| 20 | + private int norwegian; |
| 21 | + |
| 22 | + private int coffee; |
| 23 | + private int tea; |
| 24 | + private int milk; |
| 25 | + private int orangejuice; |
| 26 | + private int water; |
| 27 | + |
| 28 | + private int oldgold; |
| 29 | + private int kools; |
| 30 | + private int chesterfields; |
| 31 | + private int luckystrike; |
| 32 | + private int parliaments; |
| 33 | + |
| 34 | + private int dog; |
| 35 | + private int snails; |
| 36 | + private int fox; |
| 37 | + private int horse; |
| 38 | + private int zebra; |
| 39 | + |
| 40 | + private Map<Integer, String> nationalityNames = new HashMap<>(); |
| 41 | + |
| 42 | + private String waterDrinker = ""; |
| 43 | + private String zebraOwner = ""; |
| 44 | + |
| 45 | + ZebraPuzzle() { |
| 46 | + this.solve(); |
| 47 | + } |
| 48 | + |
| 49 | + private static boolean isJustRightOf(int neighbourA, int neighbourB) { |
| 50 | + return neighbourA - 1 == neighbourB; |
| 51 | + } |
| 52 | + |
| 53 | + private static boolean nextTo(int neighbourA, int neighbourB) { |
| 54 | + return isJustRightOf(neighbourA, neighbourB) || isJustRightOf(neighbourB, neighbourA); |
| 55 | + } |
| 56 | + |
| 57 | + private void solve() { |
| 58 | + allHouseAssignmentPermutations().forEach(this::solveHouseColors); |
| 59 | + } |
| 60 | + |
| 61 | + private void solveHouseColors(int[] colors) { |
| 62 | + red = colors[0]; |
| 63 | + green = colors[1]; |
| 64 | + ivory = colors[2]; |
| 65 | + yellow = colors[3]; |
| 66 | + blue = colors[4]; |
| 67 | + |
| 68 | + if (isJustRightOf(green, ivory)) { |
| 69 | + allHouseAssignmentPermutations().forEach(this::solveNationalities); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private void solveNationalities(int[] nationalities) { |
| 74 | + english = nationalities[0]; |
| 75 | + spaniard = nationalities[1]; |
| 76 | + ukrainian = nationalities[2]; |
| 77 | + japanese = nationalities[3]; |
| 78 | + norwegian = nationalities[4]; |
| 79 | + |
| 80 | + if (red == english |
| 81 | + && norwegian == FIRST |
| 82 | + && nextTo(norwegian, blue)) { |
| 83 | + |
| 84 | + // record names for later use |
| 85 | + nationalityNames.put(english, "Englishman"); |
| 86 | + nationalityNames.put(spaniard, "Spaniard"); |
| 87 | + nationalityNames.put(ukrainian, "Ukrainian"); |
| 88 | + nationalityNames.put(japanese, "Japanese"); |
| 89 | + nationalityNames.put(norwegian, "Norwegian"); |
| 90 | + |
| 91 | + allHouseAssignmentPermutations().forEach(this::solveBeverages); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private void solveBeverages(int[] beverages) { |
| 96 | + coffee = beverages[0]; |
| 97 | + tea = beverages[1]; |
| 98 | + milk = beverages[2]; |
| 99 | + orangejuice = beverages[3]; |
| 100 | + water = beverages[4]; |
| 101 | + |
| 102 | + if (coffee == green |
| 103 | + && ukrainian == tea |
| 104 | + && milk == MIDDLE) { |
| 105 | + allHouseAssignmentPermutations().forEach(this::solveCigars); |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + private void solveCigars(int[] cigars) { |
| 111 | + oldgold = cigars[0]; |
| 112 | + kools = cigars[1]; |
| 113 | + chesterfields = cigars[2]; |
| 114 | + luckystrike = cigars[3]; |
| 115 | + parliaments = cigars[4]; |
| 116 | + |
| 117 | + if (kools == yellow |
| 118 | + && luckystrike == orangejuice |
| 119 | + && japanese == parliaments) { |
| 120 | + allHouseAssignmentPermutations().forEach(this::solvePets); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private void solvePets(int[] pets) { |
| 125 | + dog = pets[0]; |
| 126 | + snails = pets[1]; |
| 127 | + fox = pets[2]; |
| 128 | + horse = pets[3]; |
| 129 | + zebra = pets[4]; |
| 130 | + |
| 131 | + if (spaniard == dog |
| 132 | + && oldgold == snails |
| 133 | + && nextTo(chesterfields, fox) |
| 134 | + && nextTo(kools, horse)) { |
| 135 | + this.waterDrinker = nationalityNames.get(water); |
| 136 | + this.zebraOwner = nationalityNames.get(zebra); |
| 137 | + } |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | + String getWaterDrinker() { |
| 142 | + return this.waterDrinker; |
| 143 | + } |
| 144 | + |
| 145 | + String getZebraOwner() { |
| 146 | + return this.zebraOwner; |
| 147 | + } |
| 148 | + |
| 149 | + private static void swap(int[] arr, int indexA, int indexB) { |
| 150 | + int tmp = arr[indexA]; |
| 151 | + arr[indexA] = arr[indexB]; |
| 152 | + arr[indexB] = tmp; |
| 153 | + } |
| 154 | + |
| 155 | + private static Stream.Builder<int[]> perms( |
| 156 | + int[] source, Stream.Builder<int[]> builder, int offsetLeft) { |
| 157 | + if (offsetLeft == source.length - 1) { |
| 158 | + builder.accept(Arrays.copyOf(source, source.length)); |
| 159 | + } else { |
| 160 | + for (int i = offsetLeft; i < source.length; i++) { |
| 161 | + swap(source, offsetLeft, i); |
| 162 | + perms(source, builder, offsetLeft + 1); |
| 163 | + swap(source, offsetLeft, i); |
| 164 | + } |
| 165 | + } |
| 166 | + return builder; |
| 167 | + } |
| 168 | + |
| 169 | + private static Stream<int[]> perms(int[] source) { |
| 170 | + return perms(source, Stream.builder(), 0).build(); |
| 171 | + } |
| 172 | + |
| 173 | + private static Stream<int[]> allHouseAssignmentPermutations() { |
| 174 | + return perms(new int[]{1, 2, 3, 4, 5}); |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments