-
-
Notifications
You must be signed in to change notification settings - Fork 60
Add zebra-puzzle exercise
#643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| return { | ||
| default = { | ||
| ROOT = { '.' } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # Instructions | ||
|
|
||
| Your task is to solve the Zebra Puzzle to find the answer to these two questions: | ||
|
|
||
| - Which of the residents drinks water? | ||
| - Who owns the zebra? | ||
|
|
||
| ## Puzzle | ||
|
|
||
| The following 15 statements are all known to be true: | ||
|
|
||
| 1. There are five houses. | ||
| 2. The Englishman lives in the red house. | ||
| 3. The Spaniard owns the dog. | ||
| 4. The person in the green house drinks coffee. | ||
| 5. The Ukrainian drinks tea. | ||
| 6. The green house is immediately to the right of the ivory house. | ||
| 7. The snail owner likes to go dancing. | ||
| 8. The person in the yellow house is a painter. | ||
| 9. The person in the middle house drinks milk. | ||
| 10. The Norwegian lives in the first house. | ||
| 11. The person who enjoys reading lives in the house next to the person with the fox. | ||
| 12. The painter's house is next to the house with the horse. | ||
| 13. The person who plays football drinks orange juice. | ||
| 14. The Japanese person plays chess. | ||
| 15. The Norwegian lives next to the blue house. | ||
|
|
||
| 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. | ||
|
|
||
| ~~~~exercism/note | ||
| There are 24 billion (5!⁵ = 24,883,200,000) possible solutions, so try ruling out as many solutions as possible. | ||
| ~~~~ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Introduction | ||
|
|
||
| The Zebra Puzzle is a famous logic puzzle in which there are five houses, each painted a different color. | ||
| The houses have different inhabitants, who have different nationalities, own different pets, drink different beverages and enjoy different hobbies. | ||
|
|
||
| To help you solve the puzzle, you're given 15 statements describing the solution. | ||
| However, only by combining the information in _all_ statements will you be able to find the solution to the puzzle. | ||
|
|
||
| ~~~~exercism/note | ||
| The Zebra Puzzle is a [Constraint satisfaction problem (CSP)][constraint-satisfaction-problem]. | ||
| In such a problem, you have a set of possible values and a set of constraints that limit which values are valid. | ||
| Another well-known CSP is Sudoku. | ||
|
|
||
| [constraint-satisfaction-problem]: https://en.wikipedia.org/wiki/Constraint_satisfaction_problem | ||
| ~~~~ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "authors": [ | ||
| "keiravillekode" | ||
| ], | ||
| "files": { | ||
| "solution": [ | ||
| "zebra-puzzle.lua" | ||
| ], | ||
| "test": [ | ||
| "zebra-puzzle_spec.lua" | ||
| ], | ||
| "example": [ | ||
| ".meta/example.lua" | ||
| ] | ||
| }, | ||
| "blurb": "Solve the zebra puzzle.", | ||
| "source": "Wikipedia", | ||
| "source_url": "https://en.wikipedia.org/wiki/Zebra_Puzzle" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| local zebra_puzzle = {} | ||
|
|
||
| local function permutations(a) | ||
| local n | ||
|
|
||
| return function() | ||
| if not n then | ||
| n = #a | ||
| return a | ||
| end | ||
|
|
||
| -- Step 1. Find largest j such that a[j] > a[j + 1]. | ||
| local j = n - 1 | ||
| while j >= 1 and a[j] <= a[j + 1] do | ||
| j = j - 1 | ||
| end | ||
| if j < 1 then return nil end | ||
|
|
||
| -- Step 2. Find largest l such that a[j] > a[l], then swap. | ||
| local l = n | ||
| while a[j] <= a[l] do | ||
| l = l - 1 | ||
| end | ||
| a[j], a[l] = a[l], a[j] | ||
|
|
||
| -- Step 3. Reverse a[j+1] ... a[n]. | ||
| local lo, hi = j + 1, n | ||
| while lo < hi do | ||
| a[lo], a[hi] = a[hi], a[lo] | ||
| lo = lo + 1 | ||
| hi = hi - 1 | ||
| end | ||
|
|
||
| return a | ||
| end | ||
| end | ||
|
|
||
| local function index_of(t, val) | ||
| for i, v in ipairs(t) do | ||
| if v == val then return i end | ||
| end | ||
| return nil | ||
| end | ||
|
|
||
| local function next_to(a, b) | ||
| return math.abs(a - b) == 1 | ||
| end | ||
|
|
||
| local water_drinker, zebra_owner | ||
|
|
||
| local function solve() | ||
| for colors in permutations( | ||
| { 'yellow', 'red', 'ivory', 'green', 'blue' }) do | ||
| -- 6. The green house is immediately to the right of the ivory house. | ||
| if index_of(colors, 'green') == index_of(colors, 'ivory') + 1 then | ||
| for drinks in permutations( | ||
| { 'water', 'tea', 'orange juice', 'milk', 'coffee' }) do | ||
| -- 4. Coffee is drunk in the green house. | ||
| -- 9. Milk is drunk in the middle house. | ||
| if index_of(drinks, 'coffee') == index_of(colors, 'green') | ||
| and drinks[3] == 'milk' then | ||
| for hobbies in permutations( | ||
| { 'reading', 'painting', 'football', 'dancing', 'chess' }) do | ||
| -- 8. The person in the yellow house is a painter. | ||
| -- 13. The person who plays football drinks orange juice. | ||
| if index_of(hobbies, 'painting') == index_of(colors, 'yellow') | ||
| and index_of(hobbies, 'football') == index_of(drinks, 'orange juice') then | ||
| for nationalities in permutations( | ||
| { 'Ukrainian', 'Spaniard', 'Norwegian', 'Japanese', 'Englishman' }) do | ||
| -- 10. The Norwegian lives in the first house. | ||
| -- 2. The Englishman lives in the red house. | ||
| -- 15. The Norwegian lives next to the blue house. | ||
| -- 5. The Ukrainian drinks tea. | ||
| -- 14. The Japanese person plays chess. | ||
| if nationalities[1] == 'Norwegian' | ||
| and index_of(colors, 'red') == index_of(nationalities, 'Englishman') | ||
| and next_to(index_of(nationalities, 'Norwegian'), index_of(colors, 'blue')) | ||
| and index_of(drinks, 'tea') == index_of(nationalities, 'Ukrainian') | ||
| and index_of(hobbies, 'chess') == index_of(nationalities, 'Japanese') then | ||
| for pets in permutations( | ||
| { 'zebra', 'snail', 'horse', 'fox', 'dog' }) do | ||
| -- 3. The Spaniard owns the dog. | ||
| -- 7. The snail owner likes to go dancing. | ||
| -- 11. The person who enjoys reading lives in the house next to the person with the fox. | ||
| -- 12. The painter's house is next to the house with the horse. | ||
| if index_of(pets, 'dog') == index_of(nationalities, 'Spaniard') | ||
| and index_of(pets, 'snail') == index_of(hobbies, 'dancing') | ||
| and next_to(index_of(hobbies, 'reading'), index_of(pets, 'fox')) | ||
| and next_to(index_of(hobbies, 'painting'), index_of(pets, 'horse')) then | ||
| return nationalities[index_of(drinks, 'water')], | ||
| nationalities[index_of(pets, 'zebra')] | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| local function ensure_solved() | ||
| if not water_drinker then | ||
| water_drinker, zebra_owner = solve() | ||
| end | ||
| end | ||
|
|
||
| function zebra_puzzle.drinksWater() | ||
| ensure_solved() | ||
| return water_drinker | ||
| end | ||
|
|
||
| function zebra_puzzle.ownsZebra() | ||
| ensure_solved() | ||
| return zebra_owner | ||
| end | ||
|
|
||
| return zebra_puzzle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| return { | ||
| module_name = 'zebra_puzzle', | ||
|
|
||
| generate_test = function(case) | ||
| local template = [[ | ||
| assert.equal('%s', zebra_puzzle.%s())]] | ||
| return template:format(case.expected, case.property) | ||
| end | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # This is an auto-generated file. | ||
| # | ||
| # Regenerating this file via `configlet sync` will: | ||
| # - Recreate every `description` key/value pair | ||
| # - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
| # - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
| # - Preserve any other key/value pair | ||
| # | ||
| # As user-added comments (using the # character) will be removed when this file | ||
| # is regenerated, comments can be added via a `comment` key. | ||
|
|
||
| [16efb4e4-8ad7-4d5e-ba96-e5537b66fd42] | ||
| description = "resident who drinks water" | ||
|
|
||
| [084d5b8b-24e2-40e6-b008-c800da8cd257] | ||
| description = "resident who owns zebra" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| local zebra_puzzle = {} | ||
|
|
||
| function zebra_puzzle.drinksWater() | ||
| end | ||
|
|
||
| function zebra_puzzle.ownsZebra() | ||
| end | ||
|
|
||
| return zebra_puzzle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| local zebra_puzzle = require('zebra-puzzle') | ||
|
|
||
| describe('zebra-puzzle', function() | ||
| it('resident who drinks water', function() | ||
| assert.equal('Norwegian', zebra_puzzle.drinksWater()) | ||
| end) | ||
|
|
||
| it('resident who owns zebra', function() | ||
| assert.equal('Japanese', zebra_puzzle.ownsZebra()) | ||
| end) | ||
| end) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.