Zebra puzzle: Difference between revisions
Added Uiua solution
m (→{{header|Phix}}: use pygments, and indices to a single permute set) |
(Added Uiua solution) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 4,219:
===Constraint Programming Version===
Using the rules from https://github.com/SWI-Prolog/bench/blob/master/programs/zebra.pl
<syntaxhighlight lang="julia"># Julia 1.4
using JuMP
Line 5,311:
<!--(phixonline)-->
<syntaxhighlight lang="phix">
--enum colour, nationality, drink, smoke, pet -- (now implicit)
enum red,white,green,yellow,blue
enum English,Swede,Dane,Norwegian,German
Line 5,327:
constant p5 = permutes(tagset(5)), -- all permutes of {1,2,3,4,5},
lp = length(p5) -- (== factorial(5), ie 120)
// In the following, c1,c2 are indexes to p5, for colour..pet,
// and v1,v2 are from their corresponding enums, so eg p5[c1]
// might be {1,4,3,2,5} for the colours of 1..5 and finding
// v1 in that gives us a house number. Checking the specified
// condition, eg [h] == green && [h+1] == white is then easy.
function left_of(integer c1, v1, c2, v2)
return h<=4
end function
function same_house(integer c1, v1, c2, v2)
end function
function next_to(integer c1, v1, c2, v2)
end function
procedure print_house(integer n, sequence perm)
sequence args = {n}
for i,p in perm do
Line 5,371 ⟶ 5,358:
end procedure
atom t0 = time()
for
if left_of(colour,green,colour,white) then
for
and same_house(nationality,English,colour,red)
and next_to(nationality,Norwegian,colour,blue) then
for
if same_house(nationality,Dane,drink,tea)
and same_house(drink,coffee,colour,green)
and p5[
for
perm[smoke] = s▼
if same_house(colour,yellow,smoke,Dunhill)
and same_house(nationality,German,smoke,Prince)
and same_house(smoke,BlueMaster,drink,beer)
and next_to(drink,water,smoke,Blend) then
for
if same_house(nationality,Swede,pet,dog)
and same_house(smoke,PallMall,pet,birds)
Line 5,399 ⟶ 5,381:
and next_to(pet,horse,smoke,Dunhill) then
for i=1 to 5 do
print_house(i,{colour,nationality,drink,smoke,pet})
end for
end if
end for
Line 5,412 ⟶ 5,396:
end if
end for
▲integer ns = length(solperms)
printf(1,"%d solution%s found (%3.3fs).\n",{ns,iff(ns>1,"s",""),time()-t0})
▲ printf(1,"The %s owns the Zebra\n",{nationalities[nation]})
</syntaxhighlight>
{{out}}
Line 5,427 ⟶ 5,405:
House 4: green German coffee Prince zebra
House 5: white Swede beer Blue Master dog
1 solution found (0.000s).▼
The German owns the Zebra
▲1 solution found (0.000s).
</pre>
Line 7,585 ⟶ 7,563:
</pre>
=={{header|Uiua}}==
Usese the same re-ordering of rules as a lot of other approaches. Builds and filters the list as it goes.
<syntaxhighlight lang="uiua">
Perms ← ☇1⍉∧(≡↻⇡⟜↯+1⟜⊂):¤¤°⊂⇡
Ps ← ≡⊏⊙¤Perms ⧻.
Ps "BGRWY" # Col = 0
▽⊸(=-1⊃(⊗@W|⊗@G)) # W right of G
/⊂⊞⊟ :Ps "DEGNS" # Nation = 1
▽⊸≡(=@N⊡0⊡1) # Norwegian in pos 0
▽⊸≡(=⊃(⊗@E⊡1|⊗@R⊡0)) # Red == English
▽⊸≡(=1⌵-⊃(⊗@N⊡1|⊗@B⊡0)) # Blue next to Norwegian
/⊂⊞⊂ :Ps"BCDHZ" # Pets = 2
▽⊸≡(=⊃(⊗@D⊡2|⊗@S⊡1)) # Swede owns dog
/⊂⊞⊂ :Ps"BCMTW" # Drinks = 3
▽⊸≡(=@M⊡2⊡3) # Milk in pos 2
▽⊸≡(=⊃(⊗@T⊡3|⊗@D⊡1)) # Dane drinks tea
▽⊸≡(=⊃(⊗@C⊡3|⊗@G⊡0)) # Green = Coffee
/⊂⊞⊂ :Ps"BbDpP" # Cigs = 4
▽⊸≡(=⊃(⊗@p⊡4|⊗@B⊡2)) # PallMall = Birds
▽⊸≡(=⊃(⊗@D⊡4|⊗@Y⊡0)) # Dunhill = Yellow
▽⊸≡(=⊃(⊗@b⊡4|⊗@B⊡3)) # BlueM = Beer
▽⊸≡(=⊃(⊗@P⊡4|⊗@G⊡1)) # Prince = German
▽⊸≡(=1⌵-⊃(⊗@B⊡4|⊗@C⊡2)) # Cat next to Blend
▽⊸≡(=1⌵-⊃(⊗@D⊡4|⊗@H⊡2)) # Horse next to Dunhill
▽⊸≡(=1⌵-⊃(⊗@B⊡4|⊗@W⊡3)) # Water next to Blend
&p $"Solutions found: _" ⧻.
&p$"_ owns the Z"⊡:⊡1,⊗@Z⊡2.⊢
&p"One line per house: "
&p"Col\tNat\tPet\tDrink\tCig"
≡(&p/(⊂⊂)"\t")⍉
</syntaxhighlight>
{{out}}
<pre>
Solutions found: 1
G owns the Z
One line per house:
Col Nat Pet Drink Cig
Y N C W D
B D H T B
R E B M p
G G Z C P
W S D B b
</pre>
=={{header|VBA}}==
{{trans|Phix}}
|