Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 243
Add practice exercise Zebra Puzzle#788
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
618ad72cb61bc216d083514742e3a73966ade2ccf8fe50debaa30af848d1852ebe65bef9b9f4fFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Instructions | ||
| Your task is to solve the ‘Zebra Puzzle’ also known as ‘The Einstein's Puzzle’ but how you do it that depends on you, best way to solve this problem is first you solve it with your pen and paper, it will take time but it will improve your logical thinking and its provide programmatic way to solve this problem. | ||
| **!!Spoiler warning!!** | ||
| You can learn more about this problem on [Zebra Puzzle](https://en.wikipedia.org/wiki/Zebra_Puzzle) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Instructions | ||
| Solve the zebra puzzle. | ||
| 1. There are five houses. | ||
| 2. The Englishman lives in the red house. | ||
| 3. The Spaniard owns the dog. | ||
| 4. Coffee is drunk in the green house. | ||
| 5. The Ukrainian drinks tea. | ||
| 6. The green house is immediately to the right of the ivory house. | ||
| 7. The Old Gold smoker owns snails. | ||
| 8. Kools are smoked in the yellow house. | ||
| 9. Milk is drunk in the middle house. | ||
| 10. The Norwegian lives in the first house. | ||
| 11. The man who smokes Chesterfields lives in the house next to the man with the fox. | ||
| 12. Kools are smoked in the house next to the house where the horse is kept. | ||
| 13. The Lucky Strike smoker drinks orange juice. | ||
| 14. The Japanese smokes Parliaments. | ||
| 15. The Norwegian lives next to the blue house. | ||
| 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 smoke different brands of cigarettes. | ||
| Which of the residents drinks water? | ||
| Who owns the zebra? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "authors": ["rajatnai49", "siebenschlaefer"], | ||
| "files": { | ||
| "solution": [ | ||
| "zebra_puzzle.cpp", | ||
| "zebra_puzzle.h" | ||
| ], | ||
| "test": [ | ||
| "zebra_puzzle_test.cpp" | ||
| ], | ||
| "example": [ | ||
| ".meta/example.cpp", | ||
| ".meta/example.h" | ||
| ] | ||
| }, | ||
| "blurb": "Solve the zebra puzzle.", | ||
| "source": "Wikipedia", | ||
| "source_url": "https://en.wikipedia.org/wiki/Zebra_Puzzle" | ||
| } | ||
vaeng marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,266 @@ | ||
| #include <algorithm> | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please #include the associated | ||
| #include <iostream> | ||
| #include <map> | ||
| #include <numeric> | ||
| #include <vector> | ||
| #include "zebra_puzzle.h" | ||
| using namespace std; | ||
| namespace zebra_puzzle { | ||
| enum Color { Red, Green, Ivory, Yellow, Blue }; | ||
| enum Nationality { Englishman, Spaniard, Ukrainian, Norwegian, Japanese }; | ||
| enum Pet { Dog, Snails, Fox, Horse, Zebra }; | ||
| enum Drink { Tea, Coffee, Milk, OrangeJuice, Water }; | ||
| enum Cigarette { OldGold, Kools, Chesterfields, LuckyStrike, Parliaments }; | ||
| map<Nationality, string> nationalityStrings = {{Englishman, "Englishman"}, | ||
| {Spaniard, "Spaniard"}, | ||
| {Ukrainian, "Ukrainian"}, | ||
| {Norwegian, "Norwegian"}, | ||
| {Japanese, "Japanese"}}; | ||
| struct House { | ||
| Color color; | ||
| Nationality nationality; | ||
| Pet pet; | ||
| Drink drink; | ||
| Cigarette cigarette; | ||
| }; | ||
| vector<House> houses(5); | ||
| bool check_constraints_cigarette_drink() { | ||
| for (size_t i = 0; i < houses.size(); i++) { | ||
| const auto& house = houses[i]; | ||
| // Constraint 9: Milk is drunk in the middle house. | ||
| if ((i == 2) && (house.drink != Milk)) { | ||
| return false; | ||
| } | ||
| // Constraint 13: The Lucky Strike smoker drinks orange juice. | ||
| if ((house.cigarette == LuckyStrike) && (house.drink != OrangeJuice)) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| bool check_constraints_cigarette_drink_pet() { | ||
| for (size_t i = 0; i < houses.size(); i++) { | ||
| const auto& house = houses[i]; | ||
| // Constraint 7: The Old Gold smoker owns snails. | ||
| if ((house.pet == Snails) && (house.cigarette != OldGold)) { | ||
| return false; | ||
| } | ||
| // Constraint 11: The man who smokes Chesterfields lives in the house | ||
| // next to the man with the fox. | ||
| if (((i > 0) && (house.pet == Fox) && | ||
| (houses[i - 1].cigarette != Chesterfields)) && | ||
| ((i < 4) && (house.pet == Fox) && | ||
| (houses[i + 1].cigarette != Chesterfields))) { | ||
| return false; | ||
| } | ||
| if ((i == 0) && (house.pet == Fox) && | ||
| (houses[i + 1].cigarette != Chesterfields)) { | ||
| return false; | ||
| } | ||
| if ((i == 4) && (house.pet == Fox) && | ||
| (houses[i - 1].cigarette != Chesterfields)) { | ||
| return false; | ||
| } | ||
| // Constraint 12: Kools are smoked in a house next to the house where | ||
| // the horse is kept. | ||
| if (((i > 0) && (house.pet == Horse) && | ||
| (houses[i - 1].cigarette != Kools)) && | ||
| ((i < 4) && (house.pet == Horse) && | ||
| (houses[i + 1].cigarette != Kools))) { | ||
| return false; | ||
| } | ||
| if ((i == 0) && (house.pet == Horse) && | ||
| (houses[i + 1].cigarette != Kools)) { | ||
| return false; | ||
| } | ||
| if ((i == 4) && (house.pet == Horse) && | ||
| (houses[i - 1].cigarette != Kools)) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| bool check_constraints_cigarette_drink_pet_nationality() { | ||
| for (size_t i = 0; i < houses.size(); i++) { | ||
| const auto& house = houses[i]; | ||
| // Constraint 3: The Spaniard owns the dog. | ||
| if ((house.nationality == Spaniard) && (house.pet != Dog)) { | ||
| return false; | ||
| } | ||
| // Constraint 5: The Ukrainian drinks tea. | ||
| if ((house.nationality == Ukrainian) && (house.drink != Tea)) { | ||
| return false; | ||
| } | ||
| // Constraint 10: The Norwegian lives in the first house. | ||
| if ((i == 0) && (house.nationality != Norwegian)) { | ||
| return false; | ||
| } | ||
| // Constraint 14: The Japanese smokes Parliaments. | ||
| if ((house.nationality == Japanese) && | ||
| (house.cigarette != Parliaments)) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| bool check_constraints_cigarette_drink_pet_nationality_color() { | ||
| for (size_t i = 0; i < houses.size(); i++) { | ||
| const auto& house = houses[i]; | ||
| // Constraint 2: The Englishman lives in the red house. | ||
| if ((house.color == Red) && (house.nationality != Englishman)) { | ||
| return false; | ||
| } | ||
| // Constraint 4: Coffee is drunk in the green house. | ||
| if ((house.color == Green) && (house.drink != Coffee)) { | ||
| return false; | ||
| } | ||
| // Constraint 6: The green house is immediately to the right of the | ||
| // ivory house. | ||
| if ((i > 0) && (house.color == Green) && | ||
| (houses[i - 1].color != Ivory)) { | ||
| return false; | ||
| } | ||
| if ((i == 0) && (house.color == Green)) { | ||
| return false; | ||
| } | ||
| // Constraint 8: Kools are smoked in the yellow house. | ||
| if ((house.color == Yellow) && (house.cigarette != Kools)) { | ||
| return false; | ||
| } | ||
| // Constraint 15: The Norwegian lives next to the blue house. | ||
| if (((i > 0) && (house.nationality == Norwegian) && | ||
| (houses[i - 1].color != Blue)) && | ||
| ((i < 4) && (house.nationality == Norwegian) && | ||
| (houses[i + 1].color != Blue))) { | ||
| return false; | ||
| } | ||
| if ((i == 0) && (house.nationality == Norwegian) && | ||
| (houses[i + 1].color != Blue)) { | ||
| return false; | ||
| } | ||
| if ((i == 4) && (house.nationality == Norwegian) && | ||
| (houses[i - 1].color != Blue)) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| void solve_with_permutation() { | ||
| vector<Color> colors = {Red, Green, Ivory, Yellow, Blue}; | ||
| vector<Nationality> nationalities = {Englishman, Spaniard, Ukrainian, | ||
| Norwegian, Japanese}; | ||
| vector<Pet> pets = {Dog, Snails, Fox, Horse, Zebra}; | ||
| vector<Drink> drinks = {Tea, Coffee, Milk, OrangeJuice, Water}; | ||
| vector<Cigarette> cigarettes = {OldGold, Kools, Chesterfields, LuckyStrike, | ||
| Parliaments}; | ||
| do { | ||
| for (size_t i = 0; i < 5; i++) { | ||
| houses[i].cigarette = cigarettes[i]; | ||
| } | ||
| do { | ||
| for (size_t i = 0; i < 5; i++) { | ||
| houses[i].drink = drinks[i]; | ||
| } | ||
| if (!check_constraints_cigarette_drink()) { | ||
| continue; | ||
| } | ||
| do { | ||
| for (size_t i = 0; i < 5; i++) { | ||
| houses[i].pet = pets[i]; | ||
| } | ||
| if (!check_constraints_cigarette_drink_pet()) { | ||
| continue; | ||
| } | ||
| do { | ||
| for (size_t i = 0; i < 5; i++) { | ||
| houses[i].nationality = nationalities[i]; | ||
| } | ||
| if (!check_constraints_cigarette_drink_pet_nationality()) { | ||
| continue; | ||
| } | ||
| do { | ||
| for (size_t i = 0; i < 5; i++) { | ||
| houses[i].color = colors[i]; | ||
| } | ||
| if (check_constraints_cigarette_drink_pet_nationality_color()) { | ||
| return; | ||
| } | ||
| } while (next_permutation(colors.begin(), colors.end())); | ||
| } while (next_permutation(nationalities.begin(), | ||
| nationalities.end())); | ||
| } while (next_permutation(pets.begin(), pets.end())); | ||
| } while (next_permutation(drinks.begin(), drinks.end())); | ||
| } while (next_permutation(cigarettes.begin(), cigarettes.end())); | ||
| } | ||
| Solution solve() { | ||
| solve_with_permutation(); | ||
| Solution solution; | ||
| // find the house with the zebra | ||
| for (size_t i = 0; i < houses.size(); i++) { | ||
| if (houses[i].pet == Zebra) { | ||
| solution.ownsZebra = nationalityStrings[houses[i].nationality]; | ||
| break; | ||
| } | ||
| } | ||
| // find the house with the water | ||
| for (size_t i = 0; i < houses.size(); i++) { | ||
| if (houses[i].drink == Water) { | ||
| solution.drinksWater = nationalityStrings[houses[i].nationality]; | ||
| break; | ||
| } | ||
| } | ||
| return solution; | ||
| } | ||
| } // namespace zebra_puzzle | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #if !defined(ZEBRA_PUZZLE_H) | ||
| #define ZEBRA_PUZZLE_H | ||
| #include <string> | ||
| namespace zebra_puzzle { | ||
| struct Solution { | ||
| std::string drinksWater; | ||
| std::string ownsZebra; | ||
| }; | ||
| Solution solve(); | ||
| } // namespace zebra_puzzle | ||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # 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" | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.