Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
106
Task/Zebra-puzzle/ERRE/zebra-puzzle.erre
Normal file
106
Task/Zebra-puzzle/ERRE/zebra-puzzle.erre
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
PROGRAM ZEBRA_PUZZLE
|
||||
|
||||
DIM DRINK$[4],NATION$[4],COLR$[4],SMOKE$[4],ANIMAL$[4]
|
||||
DIM PERM$[120],X$[4]
|
||||
|
||||
PROCEDURE PERMUTATION(X$[]->X$[],OK)
|
||||
LOCAL I%,J%
|
||||
FOR I%=UBOUND(X$,1)-1 TO 0 STEP -1 DO
|
||||
EXIT IF X$[I%]<X$[I%+1]
|
||||
END FOR
|
||||
IF I%<0 THEN OK=FALSE EXIT PROCEDURE END IF
|
||||
J%=UBOUND(X$,1)
|
||||
WHILE X$[J%]<=X$[I%] DO
|
||||
J%=J%-1
|
||||
END WHILE
|
||||
SWAP(X$[I%],X$[J%])
|
||||
I%=I%+1
|
||||
J%=UBOUND(X$,1)
|
||||
WHILE I%<J% DO
|
||||
SWAP(X$[I%],X$[J%])
|
||||
I%=I%+1
|
||||
J%=J%-1
|
||||
END WHILE
|
||||
OK=TRUE
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
|
||||
! The names (only used for printing the results)
|
||||
|
||||
DATA("Beer","Coffee","Milk","Tea","Water")
|
||||
DATA("Denmark","England","Germany","Norway","Sweden")
|
||||
DATA("Blue","Green","Red","White","Yellow")
|
||||
DATA("Blend","BlueMaster","Dunhill","PallMall","Prince")
|
||||
DATA("Birds","Cats","Dog","Horse","Zebra")
|
||||
|
||||
FOR I%=0 TO 4 DO READ(DRINK$[I%]) END FOR
|
||||
FOR I%=0 TO 4 DO READ(NATION$[I%]) END FOR
|
||||
FOR I%=0 TO 4 DO READ(COLR$[I%]) END FOR
|
||||
FOR I%=0 TO 4 DO READ(SMOKE$[I%]) END FOR
|
||||
FOR I%=0 TO 4 DO READ(ANIMAL$[I%]) END FOR
|
||||
|
||||
! Some single-character tags:
|
||||
A$="A" B$="B" c$="C" d$="D" e$="E"
|
||||
|
||||
! ERRE doesn't have enumerations!
|
||||
Beer$=A$ Coffee$=B$ Milk$=c$ TeA$=d$ Water$=e$
|
||||
Denmark$=A$ England$=B$ Germany$=c$ Norway$=d$ Sweden$=e$
|
||||
Blue$=A$ Green$=B$ Red$=c$ White$=d$ Yellow$=e$
|
||||
Blend$=A$ BlueMaster$=B$ Dunhill$=c$ PallMall$=d$ Prince$=e$
|
||||
Birds$=A$ Cats$=B$ Dog$=c$ Horse$=d$ ZebrA$=e$
|
||||
|
||||
PRINT(CHR$(12);)
|
||||
|
||||
! Create the 120 permutations of 5 objects:
|
||||
|
||||
X$[0]=A$ X$[1]=B$ X$[2]=C$ X$[3]=D$ X$[4]=E$
|
||||
|
||||
REPEAT
|
||||
P%=P%+1
|
||||
PERM$[P%]=X$[0]+X$[1]+X$[2]+X$[3]+X$[4]
|
||||
PERMUTATION(X$[]->X$[],OK)
|
||||
UNTIL NOT OK
|
||||
|
||||
! Solve:
|
||||
SOLUTIONS%=0
|
||||
T1=TIMER
|
||||
FOR NATION%=1 TO 120 DO
|
||||
NATION$=PERM$[NATION%]
|
||||
IF LEFT$(NATION$,1)=Norway$ THEN
|
||||
FOR COLR%=1 TO 120 DO
|
||||
COLR$=PERM$[COLR%]
|
||||
IF INSTR(COLR$,Green$+White$)<>0 AND INSTR(NATION$,England$)=INSTR(COLR$,Red$) AND ABS(INSTR(NATION$,Norway$)-INSTR(COLR$,Blue$))=1 THEN
|
||||
FOR DRINK%=1 TO 120 DO
|
||||
DRINK$=PERM$[DRINK%]
|
||||
IF MID$(DRINK$,3,1)=Milk$ AND INSTR(NATION$,Denmark$)=INSTR(DRINK$,TeA$) AND INSTR(DRINK$,Coffee$)=INSTR(COLR$,Green$) THEN
|
||||
FOR SmOKe%=1 TO 120 DO
|
||||
SmOKe$=PERM$[SMOKE%]
|
||||
IF INSTR(NATION$,Germany$)=INSTR(SmOKe$,Prince$) AND INSTR(SmOKe$,BlueMaster$)=INSTR(DRINK$,Beer$) AND ABS(INSTR(SmOKe$,Blend$)-INSTR(DRINK$,Water$))=1 AND INSTR(SmOKe$,Dunhill$)=INSTR(COLR$,Yellow$) THEN
|
||||
FOR ANIMAL%=1 TO 120 DO
|
||||
ANIMAL$=PERM$[ANIMAL%]
|
||||
IF INSTR(NATION$,Sweden$)=INSTR(ANIMAL$,Dog$) AND INSTR(SmOKe$,PallMall$)=INSTR(ANIMAL$,Birds$) AND ABS(INSTR(SmOKe$,Blend$)-INSTR(ANIMAL$,Cats$))=1 AND ABS(INSTR(SmOKe$,Dunhill$)-INSTR(ANIMAL$,Horse$))=1 THEN
|
||||
PRINT("House Drink Nation Colour Smoke Animal")
|
||||
PRINT("---------------------------------------------------------------------------")
|
||||
FOR house%=1 TO 5 DO
|
||||
PRINT(house%;)
|
||||
PRINT(TAB(10);DRINK$[ASC(MID$(DRINK$,house%))-65];)
|
||||
PRINT(TAB(25);NATION$[ASC(MID$(NATION$,house%))-65];)
|
||||
PRINT(TAB(40);COLR$[ASC(MID$(COLR$,house%))-65];)
|
||||
PRINT(TAB(55);SMOKE$[ASC(MID$(SmOKe$,house%))-65];)
|
||||
PRINT(TAB(70);ANIMAL$[ASC(MID$(ANIMAL$,house%))-65])
|
||||
END FOR
|
||||
SOLUTIONS%=SOLUTIONS%+1
|
||||
END IF
|
||||
END FOR ! ANIMAL%
|
||||
END IF
|
||||
END FOR ! SmOKe%
|
||||
END IF
|
||||
END FOR ! DRINK%
|
||||
END IF
|
||||
END FOR ! COLR%
|
||||
END IF
|
||||
END FOR ! NATION%
|
||||
PRINT("Number of solutions=";SOLUTIONS%)
|
||||
PRINT("Solved in ";TIMER-T1;" seconds")
|
||||
END PROGRAM
|
||||
72
Task/Zebra-puzzle/EchoLisp/zebra-puzzle-1.echolisp
Normal file
72
Task/Zebra-puzzle/EchoLisp/zebra-puzzle-1.echolisp
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
(lib 'hash)
|
||||
(lib 'amb)
|
||||
|
||||
;; return #f or house# for thing/category
|
||||
;; houses := (0 1 2 3 4)
|
||||
(define (house-get H category thing houses)
|
||||
(for/or ((i houses)) #:continue (!equal? (hash-ref (vector-ref H i) category) thing)
|
||||
i))
|
||||
|
||||
;; return house # for thing (eg cat) in category (eq animals)
|
||||
;; add thing if not already here
|
||||
(define-syntax-rule (house-set thing category)
|
||||
(or
|
||||
(house-get H 'category 'thing houses)
|
||||
(dispatch H 'category 'thing context houses )))
|
||||
|
||||
;; we know that thing/category is in a given house
|
||||
(define-syntax-rule (house-force thing category house)
|
||||
(dispatch H 'category 'thing context houses house))
|
||||
|
||||
;; return house# or fail if impossible
|
||||
(define (dispatch H category thing context houses (forced #f))
|
||||
(define house (or forced (amb context houses))) ;; get a house number
|
||||
(when (hash-ref (vector-ref H house) category) (amb-fail)) ;; fail if occupied
|
||||
(hash-set (vector-ref H house) category thing) ;; else remember house contents
|
||||
house)
|
||||
|
||||
(define (house-next h1 h2)
|
||||
(amb-require (or (= h1 (1+ h2)) (= h1 (1- h2)))))
|
||||
|
||||
(define (zebra-puzzle context houses )
|
||||
(define H (build-vector 5 make-hash)) ;; house[i] := hash(category) -> thing
|
||||
; In the middle house they drink milk.
|
||||
(house-force milk drinks 2)
|
||||
;The Norwegian lives in the first house.
|
||||
(house-force norvegian people 0)
|
||||
; The English man lives in the red house.
|
||||
(house-force red colors(house-set english people))
|
||||
; The Swede has a dog.
|
||||
(house-force dog animals (house-set swede people))
|
||||
; The Dane drinks tea.
|
||||
(house-force tea drinks (house-set dane people))
|
||||
; The green house is immediately to the left of the white house.
|
||||
(amb-require (= (house-set green colors) (1- (house-set white colors))))
|
||||
; They drink coffee in the green house.
|
||||
(house-force coffee drinks (house-set green colors))
|
||||
; The man who smokes Pall Mall has birds.
|
||||
(house-force birds animals (house-set pallmall smoke))
|
||||
; In the yellow house they smoke Dunhill.
|
||||
(house-force dunhill smoke (house-set yellow colors))
|
||||
; The Norwegian lives next to the blue house.
|
||||
(house-next (house-set norvegian people) (house-set blue colors))
|
||||
; The man who smokes Blend lives in the house next to the house with cats.
|
||||
(house-next (house-set blend smoke) (house-set cats animals))
|
||||
; In a house next to the house where they have a horse, they smoke Dunhill.
|
||||
(house-next (house-set horse animals) (house-set dunhill smoke))
|
||||
; The man who smokes Blue Master drinks beer.
|
||||
(house-force beer drinks (house-set bluemaster smoke))
|
||||
; The German smokes Prince.
|
||||
(house-force prince smoke (house-set german people))
|
||||
; They drink water in a house next to the house where they smoke Blend.
|
||||
(house-next (house-set water drinks) (house-set blend smoke))
|
||||
|
||||
;; Finally .... the zebra 🐴
|
||||
(house-set 🐴 animals)
|
||||
|
||||
(for ((i houses))
|
||||
(writeln i (hash-values (vector-ref H i))))
|
||||
(writeln '----------)
|
||||
|
||||
(amb-fail) ;; will ensure ALL solutions are printed
|
||||
)
|
||||
12
Task/Zebra-puzzle/EchoLisp/zebra-puzzle-2.echolisp
Normal file
12
Task/Zebra-puzzle/EchoLisp/zebra-puzzle-2.echolisp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
(define (task)
|
||||
(amb-run zebra-puzzle (amb-make-context) (iota 5)))
|
||||
|
||||
(task)
|
||||
→
|
||||
0 (norvegian yellow dunhill cats water)
|
||||
1 (dane tea blue blend horse)
|
||||
2 (milk english red pallmall birds)
|
||||
3 (green coffee german prince 🐴)
|
||||
4 (swede dog white bluemaster beer)
|
||||
----------
|
||||
→ #f
|
||||
175
Task/Zebra-puzzle/FormulaOne/zebra-puzzle.formulaone
Normal file
175
Task/Zebra-puzzle/FormulaOne/zebra-puzzle.formulaone
Normal file
|
|
@ -0,0 +1,175 @@
|
|||
// First, let's give some variables some values:
|
||||
Nationality = Englishman | Swede | Dane | Norwegian | German
|
||||
Colour = Red | Green | Yellow | Blue | White
|
||||
Cigarette = PallMall | Dunhill | BlueMaster | Blend | Prince
|
||||
Domestic = Dog | Bird | Cat | Zebra | Horse
|
||||
Beverage = Tea | Coffee | Milk | Beer | Water
|
||||
HouseOrder = First | Second | Third | Fourth | Fifth
|
||||
|
||||
{
|
||||
We use injections to make the array-elements unique.
|
||||
Example: 'Pet' is an array of unique elements of type 'Domestic', indexed by 'Nationality'.
|
||||
In the predicate 'Zebra', we use this injection 'Pet' to define the array-variable 'pet'.
|
||||
The symbol used is the '->>'. 'Nationality->>Domestic' can be read as 'Domestic(Nationality)' in "plain array-speak";
|
||||
the difference being that the elements are by definition unique.
|
||||
|
||||
So, in FormulaOne we use a formula like: 'pet(Swede) = Dog', which simply means that the 'Swede' (type 'Nationality')
|
||||
has a 'pet' (type 'Pet', which is of type 'Domestic', indexed by 'Nationality'), which appears to be a 'Dog' (type 'Domestic').
|
||||
Or, one could say that the 'Swede' has been mapped to the 'Dog' (Oh, well...).
|
||||
}
|
||||
|
||||
Pet = Nationality->>Domestic
|
||||
Drink = Nationality->>Beverage
|
||||
HouseColour = Nationality->>Colour
|
||||
Smoke = Nationality->>Cigarette
|
||||
Order = HouseOrder->>Nationality
|
||||
|
||||
pred Zebra(houseColour::HouseColour, pet::Pet, smoke::Smoke, drink::Drink, order::Order) iff
|
||||
|
||||
// For convenience sake, some temporary place_holder variables are used.
|
||||
// An underscore distinguishes them:
|
||||
|
||||
houseColour(green_house) = Green &
|
||||
houseColour(white_house) = White &
|
||||
houseColour(yellow_house) = Yellow &
|
||||
smoke(pallmall_smoker) = PallMall &
|
||||
smoke(blend_smoker) = Blend &
|
||||
smoke(dunhill_smoker) = Dunhill &
|
||||
smoke(bluemaster_smoker) = BlueMaster &
|
||||
pet(cat_keeper) = Cat &
|
||||
pet(neighbour_dunhill_smoker) = Horse &
|
||||
|
||||
{ 2. The English man lives in the red house: }
|
||||
houseColour(Englishman) = Red &
|
||||
|
||||
{ 3. The Swede has a dog: }
|
||||
pet(Swede) = Dog &
|
||||
|
||||
{ 4. The Dane drinks tea: }
|
||||
drink(Dane) = Tea &
|
||||
|
||||
{ 'smoke' and 'drink' are both nouns, like the other variables.
|
||||
One could read the formulas like: 'the colour of the Englishman's house is Red' ->
|
||||
'the Swede's pet is a dog' -> 'the Dane's drink is tea'.
|
||||
}
|
||||
|
||||
{ 5. The green house is immediately to the left of the white house: }
|
||||
{ The local predicate 'LeftOf' determines the order: }
|
||||
LeftOf(green_house, white_house, order) &
|
||||
|
||||
{ 6. They drink coffee in the green house: }
|
||||
drink(green_house) = Coffee &
|
||||
|
||||
{ 7. The man who smokes Pall Mall has birds: }
|
||||
pet(pallmall_smoker) = Bird &
|
||||
|
||||
{ 8. In the yellow house they smoke Dunhill: }
|
||||
smoke(yellow_house) = Dunhill &
|
||||
|
||||
{ 9. In the middle house they drink milk: }
|
||||
drink(order(Third)) = Milk &
|
||||
|
||||
{10. The Norwegian lives in the first house: }
|
||||
order(First) = Norwegian &
|
||||
|
||||
{11. The man who smokes Blend lives in the house next to the house with cats: }
|
||||
{ Another local predicate 'Neighbour' makes them neighbours:}
|
||||
Neighbour(blend_smoker, cat_keeper, order) &
|
||||
|
||||
{12. In a house next to the house where they have a horse, they smoke Dunhill: }
|
||||
Neighbour(dunhill_smoker, neighbour_dunhill_smoker, order) &
|
||||
|
||||
{13. The man who smokes Blue Master drinks beer: }
|
||||
drink(bluemaster_smoker) = Beer &
|
||||
|
||||
{14. The German smokes Prince: }
|
||||
smoke(German) = Prince &
|
||||
|
||||
{15. The Norwegian lives next to the blue house: }
|
||||
{10. The Norwegian lives in the first house,
|
||||
so the blue house is the second house }
|
||||
houseColour(order(Second)) = Blue &
|
||||
|
||||
{16. They drink water in a house next to the house where they smoke Blend: }
|
||||
drink(neighbour_blend_smoker) = Water &
|
||||
Neighbour(blend_smoker, neighbour_blend_smoker, order)
|
||||
|
||||
{ A simplified solution would number the houses 1, 2, 3, 4, 5
|
||||
which makes it easier to order the houses.
|
||||
'right in the center' would become 3; 'in the first house', 1
|
||||
But we stick to the original puzzle and use some local predicates.
|
||||
}
|
||||
|
||||
local pred Neighbour(neighbour1::Nationality, neighbour2::Nationality, order::Order)iff
|
||||
neighbour1 <> neighbour2 &
|
||||
order(house1) = neighbour1 &
|
||||
order(house2) = neighbour2 &
|
||||
( house1 = house2 + 1 |
|
||||
house1 = house2 - 1 )
|
||||
|
||||
local pred LeftOf(neighbour1::Nationality, neighbour2::Nationality, order::Order) iff
|
||||
neighbour1 <> neighbour2 &
|
||||
order(house1) = neighbour1 &
|
||||
order(house2) = neighbour2 &
|
||||
house1 = house2 - 1
|
||||
|
||||
{
|
||||
The 'all'-query in FormulaOne:
|
||||
all Zebra(houseColour, pet, smokes, drinks, order)
|
||||
gives, of course, only one solution, so it can be replaced by:
|
||||
one Zebra(houseColour, pet, smokes, drinks, order)
|
||||
}
|
||||
|
||||
// The compacted version:
|
||||
|
||||
Nationality = Englishman | Swede | Dane | Norwegian | German
|
||||
Colour = Red | Green | Yellow | Blue | White
|
||||
Cigarette = PallMall | Dunhill | BlueMaster | Blend | Prince
|
||||
Domestic = Dog | Bird | Cat | Zebra | Horse
|
||||
Beverage = Tea | Coffee | Milk | Beer | Water
|
||||
HouseOrder = First | Second | Third | Fourth | Fifth
|
||||
|
||||
Pet = Nationality->>Domestic
|
||||
Drink = Nationality->>Beverage
|
||||
HouseColour = Nationality->>Colour
|
||||
Smoke = Nationality->>Cigarette
|
||||
Order = HouseOrder->>Nationality
|
||||
|
||||
pred Zebra(houseColour::HouseColour, pet::Pet, smoke::Smoke, drink::Drink, order::Order) iff
|
||||
|
||||
houseColour(green_house) = Green &
|
||||
houseColour(white_house) = White &
|
||||
houseColour(yellow_house) = Yellow &
|
||||
smoke(pallmall_smoker) = PallMall &
|
||||
smoke(blend_smoker) = Blend &
|
||||
smoke(dunhill_smoker) = Dunhill &
|
||||
smoke(bluemaster_smoker) = BlueMaster &
|
||||
pet(cat_keeper) = Cat &
|
||||
pet(neighbour_dunhill_smoker) = Horse &
|
||||
|
||||
houseColour(Englishman) = Red &
|
||||
pet(Swede) = Dog &
|
||||
drink(Dane) = Tea &
|
||||
LeftOf(green_house, white_house, order) &
|
||||
drink(green_house) = Coffee &
|
||||
pet(pallmall_smoker) = Bird &
|
||||
smoke(yellow_house) = Dunhill &
|
||||
drink(order(Third)) = Milk &
|
||||
order(First) = Norwegian &
|
||||
Neighbour(blend_smoker, cat_keeper, order) &
|
||||
Neighbour(dunhill_smoker, neighbour_dunhill_smoker, order) &
|
||||
drink(bluemaster_smoker) = Beer &
|
||||
smoke(German) = Prince &
|
||||
houseColour(order(Second)) = Blue &
|
||||
drink(neighbour_blend_smoker) = Water &
|
||||
Neighbour(blend_smoker, neighbour_blend_smoker, order)
|
||||
|
||||
local pred Neighbour(neighbour1::Nationality, neighbour2::Nationality, order::Order)iff
|
||||
neighbour1 <> neighbour2 &
|
||||
order(house1) = neighbour1 & order(house2) = neighbour2 &
|
||||
( house1 = house2 + 1 | house1 = house2 - 1 )
|
||||
|
||||
local pred LeftOf(neighbour1::Nationality, neighbour2::Nationality, order::Order) iff
|
||||
neighbour1 <> neighbour2 &
|
||||
order(house1) = neighbour1 & order(house2) = neighbour2 &
|
||||
house1 = house2 - 1
|
||||
81
Task/Zebra-puzzle/Phix/zebra-puzzle.phix
Normal file
81
Task/Zebra-puzzle/Phix/zebra-puzzle.phix
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
enum Colour, Nationality, Drink, Smoke, Pet
|
||||
constant Colours = {"red","white","green","yellow","blue"},
|
||||
Nationalities = {"English","Swede","Dane","Norwegian","German"},
|
||||
Drinks = {"tea","coffee","milk","beer","water"},
|
||||
Smokes = {"Pall Mall","Dunhill","Blend","Blue Master","Prince"},
|
||||
Pets = {"dog","birds","cats","horse","zebra"},
|
||||
Sets = {Colours,Nationalities,Drinks,Smokes,Pets}
|
||||
|
||||
constant tagset5 = tagset(5) -- {1,2,3,4,5}, oft-permuted
|
||||
sequence perm = repeat(tagset5,5) -- perm[1] is Colour of each house, etc
|
||||
|
||||
function house(integer i, string name)
|
||||
return find(find(name,Sets[i]),perm[i])
|
||||
end function
|
||||
|
||||
function left_of(integer h1, integer h2)
|
||||
return (h1-h2)==1
|
||||
end function
|
||||
|
||||
function next_to(integer h1, integer h2)
|
||||
return abs(h1-h2)==1
|
||||
end function
|
||||
|
||||
procedure print_house(integer i)
|
||||
printf(1,"%d:%s,%s,%s,%s,%s\n",{i,
|
||||
Colours[perm[Colour][i]],
|
||||
Nationalities[perm[Nationality][i]],
|
||||
Drinks[perm[Drink][i]],
|
||||
Smokes[perm[Smoke][i]],
|
||||
Pets[perm[Pet][i]]})
|
||||
end procedure
|
||||
|
||||
integer solutions = 0
|
||||
sequence solperms = {}
|
||||
atom t0 = time()
|
||||
constant factorial5 = factorial(5)
|
||||
for C=1 to factorial5 do
|
||||
perm[Colour] = permute(C,tagset5)
|
||||
if left_of(house(Colour,"green"),house(Colour,"white")) then
|
||||
for N=1 to factorial5 do
|
||||
perm[Nationality] = permute(N,tagset5)
|
||||
if house(Nationality,"Norwegian")==1
|
||||
and house(Nationality,"English")==house(Colour,"red")
|
||||
and next_to(house(Nationality,"Norwegian"),house(Colour,"blue")) then
|
||||
for D=1 to factorial5 do
|
||||
perm[Drink] = permute(D,tagset5)
|
||||
if house(Nationality,"Dane")==house(Drink,"tea")
|
||||
and house(Drink,"coffee")==house(Colour,"green")
|
||||
and house(Drink,"milk")==3 then
|
||||
for S=1 to factorial5 do
|
||||
perm[Smoke] = permute(S,tagset5)
|
||||
if house(Colour,"yellow")==house(Smoke,"Dunhill")
|
||||
and house(Nationality,"German")==house(Smoke,"Prince")
|
||||
and house(Smoke,"Blue Master")==house(Drink,"beer")
|
||||
and next_to(house(Drink,"water"),house(Smoke,"Blend")) then
|
||||
for P=1 to factorial5 do
|
||||
perm[Pet] = permute(P,tagset5)
|
||||
if house(Nationality,"Swede")==house(Pet,"dog")
|
||||
and house(Smoke,"Pall Mall")==house(Pet,"birds")
|
||||
and next_to(house(Smoke,"Blend"),house(Pet,"cats"))
|
||||
and next_to(house(Pet,"horse"),house(Smoke,"Dunhill")) then
|
||||
for i=1 to 5 do
|
||||
print_house(i)
|
||||
end for
|
||||
solutions += 1
|
||||
solperms = append(solperms,perm)
|
||||
end if
|
||||
end for
|
||||
end if
|
||||
end for
|
||||
end if
|
||||
end for
|
||||
end if
|
||||
end for
|
||||
end if
|
||||
end for
|
||||
printf(1,"%d solution%s found (%3.3fs).\n",{solutions,iff(solutions>1,"s",""),time()-t0})
|
||||
for i=1 to length(solperms) do
|
||||
perm = solperms[i]
|
||||
printf(1,"The %s owns the Zebra\n",{Nationalities[house(Pet,"zebra")]})
|
||||
end for
|
||||
52
Task/Zebra-puzzle/Sidef/zebra-puzzle.sidef
Normal file
52
Task/Zebra-puzzle/Sidef/zebra-puzzle.sidef
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
var CONTENT = Hash(
|
||||
:House => nil,
|
||||
:Nationality => [:English, :Swedish, :Danish, :Norwegian, :German],
|
||||
:Colour => [:Red, :Green, :White, :Blue, :Yellow],
|
||||
:Pet => [:Dog, :Birds, :Cats, :Horse, :Zebra],
|
||||
:Drink => [:Tea, :Coffee, :Milk, :Beer, :Water],
|
||||
:Smoke => [:PallMall, :Dunhill, :BlueMaster, :Prince, :Blend]
|
||||
)
|
||||
|
||||
func adjacent(n,i,g,e) {
|
||||
(0..3).any {|x| (n[x]==i && g[x+1]==e) || (n[x+1]==i && g[x]==e) }
|
||||
}
|
||||
|
||||
func leftof(n,i,g,e) {
|
||||
(0..3).any {|x| n[x]==i && g[x+1]==e }
|
||||
}
|
||||
|
||||
func coincident(n,i,g,e) {
|
||||
n.indices.any {|x| n[x]==i && g[x]==e }
|
||||
}
|
||||
|
||||
func solve {
|
||||
CONTENT{:Nationality}.permutations{|nation|
|
||||
nation.first == :Norwegian ->
|
||||
&& CONTENT{:Colour}.permutations {|colour|
|
||||
leftof(colour,:Green,colour,:White) ->
|
||||
&& coincident(nation,:English,colour,:Red) ->
|
||||
&& adjacent(nation,:Norwegian,colour,:Blue) ->
|
||||
&& CONTENT{:Pet}.permutations {|pet|
|
||||
coincident(nation,:Swedish,pet,:Dog) ->
|
||||
&& CONTENT{:Drink}.permutations {|drink|
|
||||
drink[2] == :Milk ->
|
||||
&& coincident(nation,:Danish,drink,:Tea) ->
|
||||
&& coincident(colour,:Green,drink,:Coffee) ->
|
||||
&& CONTENT{:Smoke}.permutations {|smoke|
|
||||
coincident(smoke,:PallMall,pet,:Birds) ->
|
||||
&& coincident(smoke,:Dunhill,colour,:Yellow) ->
|
||||
&& coincident(smoke,:BlueMaster,drink,:Beer) ->
|
||||
&& coincident(smoke,:Prince,nation,:German) ->
|
||||
&& adjacent(smoke,:Blend,pet,:Cats) ->
|
||||
&& adjacent(smoke,:Blend,drink,:Water) ->
|
||||
&& adjacent(smoke,:Dunhill,pet,:Horse) ->
|
||||
&& return [nation,colour,pet,drink,smoke]
|
||||
} } } } } }
|
||||
|
||||
var res = solve();
|
||||
var keys = [:House, :Nationality, :Colour, :Pet, :Drink, :Smoke]
|
||||
var width = keys.map{ .len }
|
||||
var fmt = width.map{|w| "%-#{w+2}s" }.join(" ")
|
||||
say "The Zebra is owned by the man who is #{res[0][res[2].first_index(:Zebra)]}\n"
|
||||
say (fmt % keys..., "\n", fmt % width.map{|w| "-"*w }...)
|
||||
res[0].indices.map{|i| res.map{|a| a[i] }}.each_kv {|k,v| say fmt%(k,v...) }
|
||||
22
Task/Zebra-puzzle/jq/zebra-puzzle-1.jq
Normal file
22
Task/Zebra-puzzle/jq/zebra-puzzle-1.jq
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
# Attempt to unify the input object with the specified object
|
||||
def unify( object ):
|
||||
# Attempt to unify the input object with the specified tag:value
|
||||
def unify2(tag; value):
|
||||
if . == null then null
|
||||
elif .[tag] == value then .
|
||||
elif .[tag] == null then .[tag] = value
|
||||
else null
|
||||
end;
|
||||
reduce (object|keys[]) as $key
|
||||
(.; unify2($key; object[$key]) );
|
||||
|
||||
# Input: an array
|
||||
# Output: if the i-th element can be made to satisfy the condition,
|
||||
# then the updated array, otherwise empty.
|
||||
def enforce(i; cond):
|
||||
if 0 <= i and i < length
|
||||
then
|
||||
(.[i] | cond) as $ans
|
||||
| if $ans then .[i] = $ans else empty end
|
||||
else empty
|
||||
end ;
|
||||
60
Task/Zebra-puzzle/jq/zebra-puzzle-2.jq
Normal file
60
Task/Zebra-puzzle/jq/zebra-puzzle-2.jq
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
# Each house is a JSON object of the form:
|
||||
# { "number": _, "nation": _, "owns": _, "color": _, "drinks": _, "smokes": _}
|
||||
|
||||
# The list of houses is represented by an array of five such objects.
|
||||
|
||||
# Input: an array of objects representing houses.
|
||||
# Output: [i, solution] where i is the entity unified with obj
|
||||
# and solution is the updated array
|
||||
def solve_with_index( obj ):
|
||||
. as $Houses
|
||||
| range(0; length) as $i
|
||||
| ($Houses[$i] | unify(obj)) as $H
|
||||
| if $H then $Houses[$i] = $H else empty end
|
||||
| [ $i, .] ;
|
||||
|
||||
def solve( object ):
|
||||
solve_with_index( object )[1];
|
||||
|
||||
def adjacent( obj1; obj2 ):
|
||||
solve_with_index(obj1) as $H
|
||||
| $H[1]
|
||||
| (enforce( $H[0] - 1; unify(obj2) ),
|
||||
enforce( $H[0] + 1; unify(obj2) )) ;
|
||||
|
||||
def left_right( obj1; obj2 ):
|
||||
solve_with_index(obj1) as $H
|
||||
| $H[1]
|
||||
| enforce( $H[0] + 1; unify(obj2) ) ;
|
||||
|
||||
|
||||
# All solutions by generate-and-test
|
||||
def zebra:
|
||||
[range(0;5)] | map({"number": .}) # Five houses
|
||||
|
||||
| enforce( 0; unify( {"nation": "norwegian"} ) )
|
||||
| enforce( 2; unify( {"drinks": "milk"} ) )
|
||||
|
||||
| solve( {"nation": "englishman", "color": "red"} )
|
||||
| solve( {"nation": "swede", "owns": "dog"} )
|
||||
| solve( {"nation": "dane", "drinks": "tea"} )
|
||||
|
||||
| left_right( {"color": "green"}; {"color": "white"})
|
||||
|
||||
| solve( {"drinks": "coffee", "color": "green"} )
|
||||
| solve( {"smokes": "Pall Mall", "owns": "birds"} )
|
||||
| solve( {"color": "yellow", "smokes": "Dunhill"} )
|
||||
|
||||
| adjacent( {"smokes": "Blend" }; {"owns": "cats"} )
|
||||
| adjacent( {"owns": "horse"}; {"smokes": "Dunhill"})
|
||||
|
||||
| solve( {"drinks": "beer", "smokes": "Blue Master"} )
|
||||
| solve( {"nation": "german", "smokes": "Prince"})
|
||||
|
||||
| adjacent( {"nation": "norwegian"}; {"color": "blue"})
|
||||
| adjacent( {"drinks": "water"}; {"smokes": "Blend"})
|
||||
|
||||
| solve( {"owns": "zebra"} )
|
||||
;
|
||||
|
||||
zebra
|
||||
48
Task/Zebra-puzzle/jq/zebra-puzzle-3.jq
Normal file
48
Task/Zebra-puzzle/jq/zebra-puzzle-3.jq
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
$ time jq -n -f zebra.jq
|
||||
[
|
||||
{
|
||||
"number": 0,
|
||||
"nation": "norwegian",
|
||||
"color": "yellow",
|
||||
"smokes": "Dunhill",
|
||||
"owns": "cats",
|
||||
"drinks": "water"
|
||||
},
|
||||
{
|
||||
"number": 1,
|
||||
"drinks": "tea",
|
||||
"nation": "dane",
|
||||
"smokes": "Blend",
|
||||
"owns": "horse",
|
||||
"color": "blue"
|
||||
},
|
||||
{
|
||||
"number": 2,
|
||||
"drinks": "milk",
|
||||
"color": "red",
|
||||
"nation": "englishman",
|
||||
"owns": "birds",
|
||||
"smokes": "Pall Mall"
|
||||
},
|
||||
{
|
||||
"number": 3,
|
||||
"color": "green",
|
||||
"drinks": "coffee",
|
||||
"nation": "german",
|
||||
"smokes": "Prince",
|
||||
"owns": "zebra"
|
||||
},
|
||||
{
|
||||
"number": 4,
|
||||
"nation": "swede",
|
||||
"owns": "dog",
|
||||
"color": "white",
|
||||
"drinks": "beer",
|
||||
"smokes": "Blue Master"
|
||||
}
|
||||
]
|
||||
|
||||
# Times include compilation:
|
||||
real 0m0.284s
|
||||
user 0m0.260s
|
||||
sys 0m0.005s
|
||||
Loading…
Add table
Add a link
Reference in a new issue