Skip to content

Commit

Permalink
zebra-puzzle: add exercise (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored Mar 31, 2023
1 parent 027a326 commit bc84f03
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 1 deletion.
10 changes: 9 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,21 @@
"parsing"
]
},
{
"slug": "zebra-puzzle",
"name": "Zebra Puzzle",
"uuid": "7b7a02a8-8a52-496d-8d04-60558545d4f7",
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "collatz-conjecture",
"name": "Collatz Conjecture",
"uuid": "e6cebdbb-5b5c-4d87-a3f0-b8ec6d2438bf",
"practices": [],
"prerequisites": [],
"difficulty": 1
"difficulty": 1
}
]
},
Expand Down
24 changes: 24 additions & 0 deletions exercises/practice/zebra-puzzle/.docs/instructions.md
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?
19 changes: 19 additions & 0 deletions exercises/practice/zebra-puzzle/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"erikschierboom"
],
"files": {
"solution": [
"zebra_puzzle.pl"
],
"test": [
"zebra_puzzle_tests.plt"
],
"example": [
".meta/zebra_puzzle.example.pl"
]
},
"blurb": "Solve the zebra puzzle.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Zebra_Puzzle"
}
16 changes: 16 additions & 0 deletions exercises/practice/zebra-puzzle/.meta/tests.toml
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"
33 changes: 33 additions & 0 deletions exercises/practice/zebra-puzzle/.meta/zebra_puzzle.example.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
zebra_owner(Owner) :-
houses(Hs),
member(h(Owner,zebra,_,_,_), Hs),
!.

water_drinker(Drinker) :-
houses(Hs),
member(h(Drinker,_,_,water,_), Hs),
!.

houses(Hs) :-
% each house in the list Hs of houses is represented as:
% h(Nationality, Pet, Cigarette, Drink, Color)
length(Hs, 5), % 1
member(h(english,_,_,_,red), Hs), % 2
member(h(spanish,dog,_,_,_), Hs), % 3
member(h(_,_,_,coffee,green), Hs), % 4
member(h(ukrainian,_,_,tea,_), Hs), % 5
next(h(_,_,_,_,green), h(_,_,_,_,white), Hs), % 6
member(h(_,snake,winston,_,_), Hs), % 7
member(h(_,_,kool,_,yellow), Hs), % 8
Hs = [_,_,h(_,_,_,milk,_),_,_], % 9
Hs = [h(norwegian,_,_,_,_)|_], % 10
next(h(_,fox,_,_,_), h(_,_,chesterfield,_,_), Hs), % 11
next(h(_,_,kool,_,_), h(_,horse,_,_,_), Hs), % 12
member(h(_,_,lucky,juice,_), Hs), % 13
member(h(japanese,_,kent,_,_), Hs), % 14
next(h(norwegian,_,_,_,_), h(_,_,_,_,blue), Hs), % 15
member(h(_,_,_,water,_), Hs), % one of them drinks water
member(h(_,zebra,_,_,_), Hs). % one of them owns a zebra

next(A, B, Ls) :- append(_, [A,B|_], Ls).
next(A, B, Ls) :- append(_, [B,A|_], Ls).
3 changes: 3 additions & 0 deletions exercises/practice/zebra-puzzle/zebra_puzzle.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
zebra_owner(Owner).

water_drinker(Drinker).
15 changes: 15 additions & 0 deletions exercises/practice/zebra-puzzle/zebra_puzzle_tests.plt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pending :-
current_prolog_flag(argv, ['--all'|_]).
pending :-
write('\nA TEST IS PENDING!\n'),
fail.

:- begin_tests(zebra_puzzle).

test(resident_who_drinks_water, condition(true)) :-
water_drinker(Resident), Resident == norwegian.

test(resident_who_owns_zebra, condition(true)) :-
zebra_owner(Resident), Resident == japanese.

:- end_tests(zebra_puzzle).

0 comments on commit bc84f03

Please sign in to comment.