Skip to content

Commit 3d4a605

Browse files
authored
Add zebra-puzzle (#125)
1 parent 6b3da20 commit 3d4a605

10 files changed

Lines changed: 193 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,14 @@
737737
"practices": [],
738738
"prerequisites": [],
739739
"difficulty": 8
740+
},
741+
{
742+
"slug": "zebra-puzzle",
743+
"name": "Zebra Puzzle",
744+
"uuid": "71d0556e-4301-47e7-80c2-d3d215127746",
745+
"practices": [],
746+
"prerequisites": [],
747+
"difficulty": 8
740748
}
741749
]
742750
},
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
Lines changed: 32 additions & 0 deletions
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+
~~~~
Lines changed: 15 additions & 0 deletions
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+
~~~~
Lines changed: 19 additions & 0 deletions
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.moon"
8+
],
9+
"test": [
10+
"zebra_puzzle_spec.moon"
11+
],
12+
"example": [
13+
".meta/example.moon"
14+
]
15+
},
16+
"blurb": "Solve the zebra puzzle.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Zebra_Puzzle"
19+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
permute = require 'pl.permute' -- https://luarocks.org/modules/tieske/penlight
2+
3+
toTheRightOf = (a, b) -> a == b + 1
4+
nextTo = (a, b) -> toTheRightOf(a, b) or toTheRightOf(b, a)
5+
6+
FIRST = 1
7+
MIDDLE = 3
8+
9+
-- 1. There are five houses.
10+
-- 2. The Englishman lives in the red house.
11+
-- 3. The Spaniard owns the dog.
12+
-- 4. The person in the green house drinks coffee.
13+
-- 5. The Ukrainian drinks tea.
14+
-- 6. The green house is immediately to the right of the ivory house.
15+
-- 7. The snail owner likes to go dancing.
16+
-- 8. The person in the yellow house is a painter.
17+
-- 9. The person in the middle house drinks milk.
18+
-- 10. The Norwegian lives in the first house.
19+
-- 11. The person who enjoys reading lives in the house next to the person with the fox.
20+
-- 12. The painter's house is next to the house with the horse.
21+
-- 13. The person who plays football drinks orange juice.
22+
-- 14. The Japanese person plays chess.
23+
-- 15. The Norwegian lives next to the blue house.
24+
25+
26+
solve = ->
27+
-- solve for colours
28+
for p1 in permute.order_iter {1,2,3,4,5}
29+
{red, green, ivory, yellow, blue} = p1
30+
-- clue 6
31+
if toTheRightOf green, ivory
32+
33+
-- solve for nationalities
34+
for p2 in permute.order_iter {1,2,3,4,5}
35+
{english, spanish, ukrainian, norwegian, japanese} = p2
36+
-- clues 2, 10, 15
37+
if english == red and norwegian == FIRST and nextTo norwegian, blue
38+
nationalities = {
39+
[english]: 'EnglishMan'
40+
[spanish]: 'Spaniard'
41+
[ukrainian]: 'Ukrainian'
42+
[norwegian]: 'Norwegian'
43+
[japanese]: 'Japanese'
44+
}
45+
46+
-- solve for beverages
47+
for p3 in permute.order_iter {1,2,3,4,5}
48+
{coffee, tea, milk, orangeJuice, water} = p3
49+
-- clues 4, 5, 9
50+
if coffee == green and ukrainian == tea and milk == MIDDLE
51+
52+
-- solve for hobbies
53+
for p4 in permute.order_iter {1,2,3,4,5}
54+
{dancing, painting, reading, football, chess} = p4
55+
-- clues 8, 13, 14
56+
if painting == yellow and football == orangeJuice and japanese == chess
57+
58+
-- solve for pets
59+
for p5 in permute.order_iter {1,2,3,4,5}
60+
{dog, snails, fox, horse, zebra} = p5
61+
-- clues 3, 7, 11, 12
62+
if spanish == dog and dancing == snails and nextTo(reading, fox) and nextTo painting, horse
63+
return {
64+
waterDrinker: nationalities[water]
65+
zebraOwner: nationalities[zebra]
66+
}
67+
return waterDrinker: '?', zebraOwner: '?'
68+
69+
SOLUTION = solve!
70+
71+
{
72+
drinksWater: -> SOLUTION.waterDrinker
73+
ownsZebra: -> SOLUTION.zebraOwner
74+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
module_imports: {'drinksWater', 'ownsZebra'},
3+
4+
generate_test: (case, level) ->
5+
lines = {
6+
"assert.are.equal #{quote case.expected}, #{case.property}!"
7+
}
8+
table.concat [indent line, level for line in *lines], '\n'
9+
}
Lines changed: 16 additions & 0 deletions
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"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
drinksWater: ->
3+
error 'Implement me'
4+
5+
ownsZebra: ->
6+
error 'Implement me'
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import drinksWater, ownsZebra from require 'zebra_puzzle'
2+
3+
describe 'zebra-puzzle', ->
4+
it 'resident who drinks water', ->
5+
assert.are.equal 'Norwegian', drinksWater!
6+
7+
pending 'resident who owns zebra', ->
8+
assert.are.equal 'Japanese', ownsZebra!

0 commit comments

Comments
 (0)