-
-
Notifications
You must be signed in to change notification settings - Fork 184
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
add exercise zebra-puzzle #959
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a160126
add exercise zebra-puzzle
siebenschlaefer fb6f6a3
Merge branch 'main' into add-zebra-puzzle
ryanplusplus ccc36c5
Update exercises/practice/zebra-puzzle/zebra_puzzle.h
ryanplusplus e8ff6c3
Update config.json
ryanplusplus 8692b79
Format
ryanplusplus 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 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 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,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? |
This file contains 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,21 @@ | ||
{ | ||
"authors": [ | ||
"siebenschlaefer" | ||
], | ||
"files": { | ||
"solution": [ | ||
"zebra_puzzle.c", | ||
"zebra_puzzle.h" | ||
], | ||
"test": [ | ||
"test_zebra_puzzle.c" | ||
], | ||
"example": [ | ||
".meta/example.c", | ||
".meta/example.h" | ||
] | ||
}, | ||
"blurb": "Solve the zebra puzzle.", | ||
"source": "Wikipedia", | ||
"source_url": "https://en.wikipedia.org/wiki/Zebra_Puzzle" | ||
} |
This file contains 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,203 @@ | ||
#include "zebra_puzzle.h" | ||
#include <assert.h> | ||
#include <stdbool.h> | ||
#include <stddef.h> | ||
#include <stdlib.h> | ||
|
||
enum { NUM_HOUSES = 5 }; | ||
|
||
static inline void swap(int *a, int *b) | ||
{ | ||
int tmp = *a; | ||
*a = *b; | ||
*b = tmp; | ||
} | ||
|
||
/// Transform `order` into its next lexicographically larger permutation. | ||
/// | ||
/// return true if on success | ||
/// return false if parameter is already the largest possible permutation | ||
static bool next_permutation(int order[NUM_HOUSES], int by_value[NUM_HOUSES]) | ||
{ | ||
const size_t len = NUM_HOUSES; | ||
|
||
// find the rightmost element that is smaller than its right neighbor | ||
size_t idx1 = len - 2; | ||
while (idx1 < len && order[idx1] >= order[idx1 + 1]) | ||
--idx1; | ||
|
||
// if no such element has been found we've reached the final permutation | ||
if (idx1 >= len) | ||
return false; | ||
|
||
// find the rightmost element that is larger than order[idx1] | ||
size_t idx2 = len - 1; | ||
while (order[idx1] >= order[idx2]) | ||
--idx2; | ||
|
||
// permute the elements | ||
swap(order + idx1, order + idx2); | ||
for (size_t a = idx1 + 1, b = len - 1; a < b; ++a, --b) | ||
swap(order + a, order + b); | ||
|
||
// update the affected elements of by_value | ||
for (size_t i = idx1; i < NUM_HOUSES; ++i) | ||
by_value[order[i]] = i; | ||
return true; | ||
} | ||
|
||
enum nationality { ENGLISH, JAPANESE, NORWEGIAN, SPANISH, UKRAINIAN }; | ||
enum color { BLUE, GREEN, IVORY, RED, YELLOW }; | ||
enum animal { DOG, FOX, HORSE, SNAILS, ZEBRA }; | ||
enum drink { COFFEE, MILK, ORANGE_JUICE, TEA, WATER }; | ||
enum smoke { CHESTERFIELD, KOOL, LUCKY_STRIKE, OLD_GOLD, PARLIAMENT }; | ||
|
||
static void solve(const char **out_drinks_water, const char **out_owns_zebra) | ||
{ | ||
int num_solutions = 0; | ||
int drinks_water = -1; | ||
int owns_zebra = -1; | ||
|
||
int colors[] = { BLUE, GREEN, IVORY, RED, YELLOW }; | ||
int house_by_color[] = { 0, 1, 2, 3, 4 }; | ||
do { | ||
// 6. The green house is immediately to the right of the ivory house. | ||
if (house_by_color[GREEN] - 1 != house_by_color[IVORY]) | ||
continue; | ||
|
||
int nationalities[] = { ENGLISH, JAPANESE, NORWEGIAN, SPANISH, | ||
UKRAINIAN }; | ||
int house_by_nationality[] = { 0, 1, 2, 3, 4 }; | ||
do { | ||
// 2. The Englishman lives in the red house. | ||
if (house_by_nationality[ENGLISH] != house_by_color[RED]) | ||
continue; | ||
|
||
// 10. The Norwegian lives in the first house. | ||
if (house_by_nationality[NORWEGIAN] != 0) | ||
continue; | ||
|
||
// 15. The Norwegian lives next to the blue house. | ||
if (abs(house_by_nationality[NORWEGIAN] - house_by_color[BLUE]) != 1) | ||
continue; | ||
|
||
int pets[] = { DOG, FOX, HORSE, SNAILS, ZEBRA }; | ||
int house_by_pet[] = { 0, 1, 2, 3, 4 }; | ||
do { | ||
// 3. The Spaniard owns the dog. | ||
if (house_by_nationality[SPANISH] != house_by_pet[DOG]) | ||
continue; | ||
|
||
int beverages[] = { COFFEE, MILK, ORANGE_JUICE, TEA, WATER }; | ||
int house_by_beverage[] = { 0, 1, 2, 3, 4 }; | ||
do { | ||
// 4. Coffee is drunk in the green house. | ||
if (house_by_beverage[COFFEE] != house_by_color[GREEN]) | ||
continue; | ||
|
||
// 5. The Ukrainian drinks tea. | ||
if (house_by_nationality[UKRAINIAN] != house_by_beverage[TEA]) | ||
continue; | ||
|
||
// 9. Milk is drunk in the middle house. | ||
if (house_by_beverage[MILK] != NUM_HOUSES / 2) | ||
continue; | ||
|
||
int brands[] = { CHESTERFIELD, KOOL, LUCKY_STRIKE, OLD_GOLD, | ||
PARLIAMENT }; | ||
int house_by_brand[] = { 0, 1, 2, 3, 4 }; | ||
do { | ||
// 7. The Old Gold smoker owns snails. | ||
if (house_by_brand[OLD_GOLD] != house_by_pet[SNAILS]) | ||
continue; | ||
|
||
// 8. Kools are smoked in the yellow house. | ||
if (house_by_brand[KOOL] != house_by_color[YELLOW]) | ||
continue; | ||
|
||
// 11. The man who smokes Chesterfields lives in the | ||
// house next to the man with the fox. | ||
if (abs(house_by_brand[CHESTERFIELD] - house_by_pet[FOX]) != | ||
1) | ||
continue; | ||
|
||
// 12. Kools are smoked in the house next to the house | ||
// where the horse is kept. | ||
if (abs(house_by_brand[KOOL] - house_by_pet[HORSE]) != 1) | ||
continue; | ||
|
||
// 13. The Lucky Strike smoker drinks orange juice. | ||
if (house_by_brand[LUCKY_STRIKE] != | ||
house_by_beverage[ORANGE_JUICE]) | ||
continue; | ||
|
||
// 14. The Japanese smokes Parliaments. | ||
if (house_by_nationality[JAPANESE] != | ||
house_by_brand[PARLIAMENT]) | ||
continue; | ||
|
||
assert(num_solutions == 0); | ||
++num_solutions; | ||
drinks_water = nationalities[house_by_beverage[WATER]]; | ||
owns_zebra = nationalities[house_by_pet[ZEBRA]]; | ||
|
||
} while (next_permutation(brands, house_by_brand)); | ||
} while (next_permutation(beverages, house_by_beverage)); | ||
} while (next_permutation(pets, house_by_pet)); | ||
} while (next_permutation(nationalities, house_by_nationality)); | ||
} while (next_permutation(colors, house_by_color)); | ||
|
||
assert(num_solutions == 1); | ||
switch (drinks_water) { | ||
case ENGLISH: | ||
*out_drinks_water = "English"; | ||
break; | ||
case JAPANESE: | ||
*out_drinks_water = "Japanese"; | ||
break; | ||
case NORWEGIAN: | ||
*out_drinks_water = "Norwegian"; | ||
break; | ||
case SPANISH: | ||
*out_drinks_water = "Spanish"; | ||
break; | ||
case UKRAINIAN: | ||
*out_drinks_water = "Ukrainian"; | ||
break; | ||
default: | ||
assert(false); | ||
} | ||
switch (owns_zebra) { | ||
case ENGLISH: | ||
*out_owns_zebra = "English"; | ||
break; | ||
case JAPANESE: | ||
*out_owns_zebra = "Japanese"; | ||
break; | ||
case NORWEGIAN: | ||
*out_owns_zebra = "Norwegian"; | ||
break; | ||
case SPANISH: | ||
*out_owns_zebra = "Spanish"; | ||
break; | ||
case UKRAINIAN: | ||
*out_owns_zebra = "Ukrainian"; | ||
break; | ||
default: | ||
assert(false); | ||
} | ||
} | ||
|
||
const char *drinks_water(void) | ||
{ | ||
const char *result, *dummy; | ||
solve(&result, &dummy); | ||
return result; | ||
} | ||
|
||
const char *owns_zebra(void) | ||
{ | ||
const char *result, *dummy; | ||
solve(&dummy, &result); | ||
return result; | ||
} |
This file contains 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,10 @@ | ||
#ifndef ZEBRA_PUZZLE_H | ||
#define ZEBRA_PUZZLE_H | ||
|
||
/// Determine the nationality of the resident who drinks water. | ||
const char *drinks_water(void); | ||
|
||
/// Determine the nationality of the resident who owns the zebra. | ||
const char *owns_zebra(void); | ||
|
||
#endif |
This file contains 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 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,37 @@ | ||
### If you wish to use extra libraries (math.h for instance), | ||
### add their flags here (-lm in our case) in the "LIBS" variable. | ||
|
||
LIBS = -lm | ||
|
||
### | ||
CFLAGS = -std=c99 | ||
CFLAGS += -g | ||
CFLAGS += -Wall | ||
CFLAGS += -Wextra | ||
CFLAGS += -pedantic | ||
CFLAGS += -Werror | ||
CFLAGS += -Wmissing-declarations | ||
CFLAGS += -DUNITY_SUPPORT_64 -DUNITY_OUTPUT_COLOR | ||
|
||
ASANFLAGS = -fsanitize=address | ||
ASANFLAGS += -fno-common | ||
ASANFLAGS += -fno-omit-frame-pointer | ||
|
||
.PHONY: test | ||
test: tests.out | ||
@./tests.out | ||
|
||
.PHONY: memcheck | ||
memcheck: ./*.c ./*.h | ||
@echo Compiling $@ | ||
@$(CC) $(ASANFLAGS) $(CFLAGS) test-framework/unity.c ./*.c -o memcheck.out $(LIBS) | ||
@./memcheck.out | ||
@echo "Memory check passed" | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -rf *.o *.out *.out.dSYM | ||
|
||
tests.out: ./*.c ./*.h | ||
@echo Compiling $@ | ||
@$(CC) $(CFLAGS) test-framework/unity.c ./*.c -o tests.out $(LIBS) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why solve the same problem twice?
Is it possible to solve it once and then just query the results (possibly in some generic way) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean to take an object oriented approach to the exercise where solving and querying the solution are separate steps?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, and don't like it either.
I was under the impression that these two separate functions would be a what the author of the problem-specification intended, with the two "properties"
drinksWater
andownsZebra
.I looked at the C# version where those are two
static
methods and the Python version where those are two free functions.But now that you asked I dug a little further and found the Go version that has a single function
SolvePuzzle()
which I like more.I could change the code to something like