Skip to content

Commit 622f512

Browse files
committed
Add zebra-puzzle exercise
1 parent 1bac5c2 commit 622f512

10 files changed

+256
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,14 @@
739739
"practices": [],
740740
"prerequisites": [],
741741
"difficulty": 7
742+
},
743+
{
744+
"slug": "zebra-puzzle",
745+
"name": "Zebra Puzzle",
746+
"uuid": "be437e7f-5dc0-4c99-a4e0-1eb5b7cdbc1a",
747+
"practices": [],
748+
"prerequisites": [],
749+
"difficulty": 9
742750
}
743751
],
744752
"foregone": [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Instructions
2+
3+
Your task is to solve the Zebra Puzzle to find the answer to these two questions:
4+
5+
- Which of the residents drinks water?
6+
- Who owns the zebra?
7+
8+
## Puzzle
9+
10+
The following 15 statements are all known to be true:
11+
12+
1. There are five houses.
13+
2. The Englishman lives in the red house.
14+
3. The Spaniard owns the dog.
15+
4. The person in the green house drinks coffee.
16+
5. The Ukrainian drinks tea.
17+
6. The green house is immediately to the right of the ivory house.
18+
7. The snail owner likes to go dancing.
19+
8. The person in the yellow house is a painter.
20+
9. The person in the middle house drinks milk.
21+
10. The Norwegian lives in the first house.
22+
11. The person who enjoys reading lives in the house next to the person with the fox.
23+
12. The painter's house is next to the house with the horse.
24+
13. The person who plays football drinks orange juice.
25+
14. The Japanese person plays chess.
26+
15. The Norwegian lives next to the blue house.
27+
28+
Additionally, 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 engage in different hobbies.
29+
30+
~~~~exercism/note
31+
There are 24 billion (5!⁵ = 24,883,200,000) possible solutions, so try ruling out as many solutions as possible.
32+
~~~~
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Introduction
2+
3+
The Zebra Puzzle is a famous logic puzzle in which there are five houses, each painted a different color.
4+
The houses have different inhabitants, who have different nationalities, own different pets, drink different beverages and enjoy different hobbies.
5+
6+
To help you solve the puzzle, you're given 15 statements describing the solution.
7+
However, only by combining the information in _all_ statements will you be able to find the solution to the puzzle.
8+
9+
~~~~exercism/note
10+
The Zebra Puzzle is a [Constraint satisfaction problem (CSP)][constraint-satisfaction-problem].
11+
In such a problem, you have a set of possible values and a set of constraints that limit which values are valid.
12+
Another well-known CSP is Sudoku.
13+
14+
[constraint-satisfaction-problem]: https://en.wikipedia.org/wiki/Constraint_satisfaction_problem
15+
~~~~
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"zebra-puzzle.wren"
8+
],
9+
"test": [
10+
"zebra-puzzle.spec.wren"
11+
],
12+
"example": [
13+
".meta/proof.ci.wren"
14+
]
15+
},
16+
"blurb": "Solve the zebra puzzle.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Zebra_Puzzle"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
class ZebraPuzzle {
2+
construct new() {
3+
_waterDrinker = ""
4+
_zebraOwner = ""
5+
solve
6+
}
7+
8+
drinksWater { _waterDrinker }
9+
ownsZebra { _zebraOwner }
10+
11+
rightOf(a, b) { a + 1 == b }
12+
nextTo(a, b) { rightOf(a, b) || rightOf(b, a) }
13+
14+
solve {
15+
var HOUSES = [1,2,3,4,5]
16+
var FIRST = 1
17+
var MIDDLE = 3
18+
19+
var permutations = [
20+
[1,2,3,4,5], [1,2,3,5,4], [1,2,4,3,5], [1,2,4,5,3], [1,2,5,3,4], [1,2,5,4,3],
21+
[1,3,2,4,5], [1,3,2,5,4], [1,3,4,2,5], [1,3,4,5,2], [1,3,5,2,4], [1,3,5,4,2],
22+
[1,4,2,3,5], [1,4,2,5,3], [1,4,3,2,5], [1,4,3,5,2], [1,4,5,2,3], [1,4,5,3,2],
23+
[1,5,2,3,4], [1,5,2,4,3], [1,5,3,2,4], [1,5,3,4,2], [1,5,4,2,3], [1,5,4,3,2],
24+
[2,1,3,4,5], [2,1,3,5,4], [2,1,4,3,5], [2,1,4,5,3], [2,1,5,3,4], [2,1,5,4,3],
25+
[2,3,1,4,5], [2,3,1,5,4], [2,3,4,1,5], [2,3,4,5,1], [2,3,5,1,4], [2,3,5,4,1],
26+
[2,4,1,3,5], [2,4,1,5,3], [2,4,3,1,5], [2,4,3,5,1], [2,4,5,1,3], [2,4,5,3,1],
27+
[2,5,1,3,4], [2,5,1,4,3], [2,5,3,1,4], [2,5,3,4,1], [2,5,4,1,3], [2,5,4,3,1],
28+
[3,1,2,4,5], [3,1,2,5,4], [3,1,4,2,5], [3,1,4,5,2], [3,1,5,2,4], [3,1,5,4,2],
29+
[3,2,1,4,5], [3,2,1,5,4], [3,2,4,1,5], [3,2,4,5,1], [3,2,5,1,4], [3,2,5,4,1],
30+
[3,4,1,2,5], [3,4,1,5,2], [3,4,2,1,5], [3,4,2,5,1], [3,4,5,1,2], [3,4,5,2,1],
31+
[3,5,1,2,4], [3,5,1,4,2], [3,5,2,1,4], [3,5,2,4,1], [3,5,4,1,2], [3,5,4,2,1],
32+
[4,1,2,3,5], [4,1,2,5,3], [4,1,3,2,5], [4,1,3,5,2], [4,1,5,2,3], [4,1,5,3,2],
33+
[4,2,1,3,5], [4,2,1,5,3], [4,2,3,1,5], [4,2,3,5,1], [4,2,5,1,3], [4,2,5,3,1],
34+
[4,3,1,2,5], [4,3,1,5,2], [4,3,2,1,5], [4,3,2,5,1], [4,3,5,1,2], [4,3,5,2,1],
35+
[4,5,1,2,3], [4,5,1,3,2], [4,5,2,1,3], [4,5,2,3,1], [4,5,3,1,2], [4,5,3,2,1],
36+
[5,1,2,3,4], [5,1,2,4,3], [5,1,3,2,4], [5,1,3,4,2], [5,1,4,2,3], [5,1,4,3,2],
37+
[5,2,1,3,4], [5,2,1,4,3], [5,2,3,1,4], [5,2,3,4,1], [5,2,4,1,3], [5,2,4,3,1],
38+
[5,3,1,2,4], [5,3,1,4,2], [5,3,2,1,4], [5,3,2,4,1], [5,3,4,1,2], [5,3,4,2,1],
39+
[5,4,1,2,3], [5,4,1,3,2], [5,4,2,1,3], [5,4,2,3,1], [5,4,3,1,2], [5,4,3,2,1]
40+
]
41+
42+
for (colours in permutations) {
43+
var blue = colours[0]
44+
var green = colours[1]
45+
var ivory = colours[2]
46+
var red = colours[3]
47+
var yellow = colours[4]
48+
49+
if (rightOf(green, ivory)) {
50+
51+
for (nations in permutations) {
52+
var en = nations[0]
53+
var es = nations[1]
54+
var ja = nations[2]
55+
var no = nations[3]
56+
var uk = nations[4]
57+
58+
var nationalities = ["","","","","",""]
59+
nationalities[en] = "English"
60+
nationalities[es] = "Spanish"
61+
nationalities[ja] = "Japanese"
62+
nationalities[no] = "Norwegian"
63+
nationalities[uk] = "Ukranian"
64+
65+
if (en == red && no == FIRST && nextTo(no, blue)) {
66+
67+
for (drinks in permutations) {
68+
var coffee = drinks[0]
69+
var tea = drinks[1]
70+
var milk = drinks[2]
71+
var juice = drinks[3]
72+
var water = drinks[4]
73+
74+
if (coffee == green && uk == tea && milk == MIDDLE) {
75+
76+
for (hobbies in permutations) {
77+
var dance = hobbies[0]
78+
var paint = hobbies[1]
79+
var read = hobbies[2]
80+
var chess = hobbies[3]
81+
var football = hobbies[4]
82+
83+
if (paint == yellow && football == juice && ja == chess) {
84+
85+
for (pets in permutations) {
86+
var horse = pets[0]
87+
var fox = pets[1]
88+
var snail = pets[2]
89+
var dog = pets[3]
90+
var zebra = pets[4]
91+
92+
if (es == dog && dance == snail && nextTo(read, fox) && nextTo(paint, horse)) {
93+
94+
_waterDrinker = nationalities[water]
95+
_zebraOwner = nationalities[zebra]
96+
return
97+
}
98+
}
99+
}
100+
}
101+
}
102+
}
103+
}
104+
}
105+
}
106+
}
107+
}
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[16efb4e4-8ad7-4d5e-ba96-e5537b66fd42]
13+
description = "resident who drinks water"
14+
15+
[084d5b8b-24e2-40e6-b008-c800da8cd257]
16+
description = "resident who owns zebra"
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Exercism
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import "wren-package" for WrenPackage, Dependency
2+
import "os" for Process
3+
4+
class Package is WrenPackage {
5+
construct new() {}
6+
name { "exercism/zebra-puzzle" }
7+
dependencies {
8+
return [
9+
Dependency.new("wren-testie", "0.3.0", "https://github.com/joshgoebel/wren-testie.git")
10+
]
11+
}
12+
}
13+
14+
Package.new().default()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import "./zebra-puzzle" for ZebraPuzzle
2+
import "wren-testie/testie" for Testie, Expect
3+
4+
Testie.test("ZebraPuzzle") { |do, skip|
5+
do.test("resident who drinks water") {
6+
var puzzle = ZebraPuzzle.new()
7+
var actual = puzzle.drinksWater
8+
var expected = "Norwegian"
9+
Expect.value(actual).toEqual(expected)
10+
}
11+
12+
skip.test("resident who owns zebra") {
13+
var puzzle = ZebraPuzzle.new()
14+
var actual = puzzle.ownsZebra
15+
var expected = "Japanese"
16+
Expect.value(actual).toEqual(expected)
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class ZebraPuzzle {
2+
construct new() {
3+
Fiber.abort("Remove this statement and implement this function")
4+
}
5+
}

0 commit comments

Comments
 (0)