Skip to content

Commit 66dbda5

Browse files
lemoncurryFridaTveit
authored andcommitted
zebra-puzzle: Add exercise (exercism#1594)
* Add new exercise zebra-puzzle Co-Authored-By: Jonas Weber <thriqon@users.noreply.github.com> * Fix ZebraPuzzle.java missing variable Co-Authored-By: Jonas Weber <thriqon@users.noreply.github.com> * Fix style issues * Change exercise topics and difficulty level * zebra-puzzle: restructure to use private methods
1 parent c2ca198 commit 66dbda5

8 files changed

Lines changed: 272 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,16 @@
961961
"loops"
962962
]
963963
},
964+
{
965+
"slug": "zebra-puzzle",
966+
"uuid": "b1e2bd39-3f4b-44c1-b7e2-258d4ee241f8",
967+
"core": false,
968+
"unlocked_by": "saddle-points",
969+
"difficulty": 7,
970+
"topics": [
971+
"logic"
972+
]
973+
},
964974
{
965975
"slug": "anagram",
966976
"uuid": "fb10dc2f-0ba1-44b6-8365-5e48e86d1283",

‎exercises/settings.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,5 @@ include 'word-count'
102102
include 'word-search'
103103
include 'wordy'
104104
include 'yacht'
105+
include 'zebra-puzzle'
105106
include 'zipper'
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.1.0

‎exercises/zebra-puzzle/README.md‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Zebra Puzzle
2+
3+
Solve the zebra puzzle.
4+
5+
1. There are five houses.
6+
2. The Englishman lives in the red house.
7+
3. The Spaniard owns the dog.
8+
4. Coffee is drunk in the green house.
9+
5. The Ukrainian drinks tea.
10+
6. The green house is immediately to the right of the ivory house.
11+
7. The Old Gold smoker owns snails.
12+
8. Kools are smoked in the yellow house.
13+
9. Milk is drunk in the middle house.
14+
10. The Norwegian lives in the first house.
15+
11. The man who smokes Chesterfields lives in the house next to the man with the fox.
16+
12. Kools are smoked in the house next to the house where the horse is kept.
17+
13. The Lucky Strike smoker drinks orange juice.
18+
14. The Japanese smokes Parliaments.
19+
15. The Norwegian lives next to the blue house.
20+
21+
Each of the five houses is painted a different color, and their
22+
inhabitants are of different national extractions, own different pets,
23+
drink different beverages and smoke different brands of cigarettes.
24+
25+
Which of the residents drinks water?
26+
Who owns the zebra?
27+
28+
# Running the tests
29+
30+
You can run all the tests for an exercise by entering
31+
32+
```sh
33+
$ gradle test
34+
```
35+
36+
in your terminal.
37+
38+
## Source
39+
40+
Wikipedia [https://en.wikipedia.org/wiki/Zebra_Puzzle](https://en.wikipedia.org/wiki/Zebra_Puzzle)
41+
42+
## Submitting Incomplete Solutions
43+
44+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.12"
11+
}
12+
13+
test {
14+
testLogging {
15+
exceptionFormat = 'full'
16+
events = ["passed", "failed", "skipped"]
17+
}
18+
}

‎exercises/zebra-puzzle/src/main/java/.keep‎

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import org.junit.Ignore;
2+
import org.junit.Test;
3+
4+
import static org.junit.Assert.assertEquals;
5+
6+
public class ZebraPuzzleTest {
7+
8+
@Test
9+
public void residentWhoDrinksWater() {
10+
ZebraPuzzle zebraPuzzle = new ZebraPuzzle();
11+
assertEquals("Norwegian", zebraPuzzle.getWaterDrinker());
12+
}
13+
14+
@Ignore("Remove to run test")
15+
@Test
16+
public void residentWhoOwnsZebra() {
17+
ZebraPuzzle zebraPuzzle = new ZebraPuzzle();
18+
assertEquals("Japanese", zebraPuzzle.getZebraOwner());
19+
}
20+
21+
}

0 commit comments

Comments
 (0)