WindowTitle "Zebra-Rätsel"
'Aus Rosetta code Zebra puzzle BBC-Basic nach XProfan-11.2a INTERPRETER ONLY!
'Q: http://rosettacode.org/wiki/Zebra_puzzle#BBC_BASIC
'(CL) CopyLeft 2018-06 by p.specht, Vienna/Austria; OHNE JEDE GEWÄHR!
WindowStyle 24:cls
$IFNDEF interpreter
print "\n\n\n\n I N T E R P R E T E R O N L Y !"
waitinput 10000:END:ProgEnd
$ENDIF
REM The names (only used for printing the results):
Declare Drink$[4], Nation$[4], Colr$[4], Smoke$[4], Animal$[4]
Drink$[] = substr$("Beer,Coffee,Milk,Tea,Water",&index+1,",")
Nation$[] = substr$("Denmark,England,Germany,Norway,Sweden",&index+1,",")
Colr$[] = substr$("Blue,Green,Red,White,Yellow",&index+1,",")
Smoke$[] = substr$("Blend,BlueMaster,Dunhill,PallMall,Prince",&index+1,",")
Animal$[] = substr$("Birds,Cats,Dog,Horse,ZEBRA",&index+1,",")
REM Some single-character tags:
declare a$,b$,c$,d$,e$ :a$="A":b$="B":c$="C":d$="D":e$="E"
REM Since XProfan-11.2a doesn't have internal enumerations:
var Beer$=a$:var Coffee$=b$:var Milk$=c$:var Tea$=d$ :var Water$=e$
var Denmark$=a$:var England$=b$:var Germany$=c$:var Norway$=d$:var Sweden$=e$
var Blue$=a$:var Green$=b$:var Red$=c$:var White$=d$:var Yellow$=e$
var Blend$=a$:var BlueMaster$=b$:var Dunhill$=c$:var PallMall$=d$:var Prince$=e$
var Birds$=a$:var Cats$=b$:var Dog$=c$:var Horse$=d$:var Zebra$=e$
REM Create the 5!=120 permutations of 5 objects:
Declare perm$[120],x$[4],p%,tmp$
x$[0]=a$:x$[1]=b$:X$[2]=c$:x$[3]=d$:x$[4]=e$
REPEAT
inc p%
perm$[p%] = x$[0]+x$[1]+x$[2]+x$[3]+x$[4]
UNTIL NOT(FNperm(x$[]))
REM Express the statements as conditional expressions:
declare ex2$,ex3$,ex4$,ex5$,ex6$,ex7$,ex8$,ex9$
declare ex10$,ex11$,ex12$,ex13$,ex14$,ex15$,ex16$
declare solutions%,tm&
declare nation%,colr%,drink%,smoke%,animal%,house%
declare nation$,colr$,drink$,smoke$,animal$
'The English man lives in the red house.
ex2$ = "INSTR(England$,Nation$) = INSTR(Red$,Colr$)"
'The Swede has a dog.
ex3$ = "INSTR(Sweden$,Nation$) = INSTR(Dog$,Animal$)"
'The Dane drinks tea.
ex4$ = "INSTR(Denmark$,Nation$) = INSTR(Tea$,Drink$)"
'The green house is immediately to the left of the white house.
ex5$ = "INSTR(Green$+White$,Colr$) <> 0"
'They drink coffee in the green house.
ex6$ = "INSTR(Coffee$,Drink$) = INSTR(Green$,Colr$)"
'The man who smokes Pall Mall has birds.
ex7$ = "INSTR(PallMall$,Smoke$) = INSTR(Birds$,Animal$)"
'In the yellow house they smoke Dunhill.
ex8$ = "INSTR(Dunhill$,Smoke$) = INSTR(Yellow$,Colr$)"
'In the middle house they drink milk.
ex9$ = "MID$(Drink$,3,1) = Milk$"
'The Norwegian lives in the first house.
ex10$ = "LEFT$(Nation$,1) = Norway$"
'The man who smokes Blend lives in the house next to the house with cats.
ex11$ = "ABS(INSTR(Blend$,Smoke$)-INSTR(Cats$,Animal$)) = 1"
'In a house next to the house where they have a horse, they smoke Dunhill.
ex12$ = "ABS(INSTR(Dunhill$,Smoke$)-INSTR(Horse$,Animal$)) = 1"
'The man who smokes Blue Master drinks beer.
ex13$ = "INSTR(BlueMaster$,Smoke$) = INSTR(Beer$,Drink$)"
'The German smokes Prince.
ex14$ = "INSTR(Germany$,Nation$) = INSTR(Prince$,Smoke$)"
'The Norwegian lives next to the blue house.
ex15$ = "ABS(INSTR(Norway$,Nation$)-INSTR(Blue$,Colr$)) = 1"
'They drink water in a house next to the house where they smoke Blend.
ex16$ = "ABS(INSTR(Blend$,Smoke$)-INSTR(Water$,Drink$)) = 1"
REM ***************** Solver *********************
' The question is, who owns the zebra?
'Additionally, list the solution for all the houses. Optionally, show the solution is unique.
solutions% = 0
tm& = &gettickcount
whileloop 120:nation%=&Loop:Nation$ = perm$[nation%]
IF EVAL(ex10$)
Whileloop 120:colr%=&Loop:Colr$ = perm$[colr%]
IF EVAL(ex5$)
IF EVAL(ex2$)
IF EVAL(ex15$)
whileloop 120:drink%=&Loop:Drink$ = perm$[drink%]
IF EVAL(ex9$)
IF EVAL(ex4$)
IF EVAL(ex6$)
whileloop 120:smoke%=&Loop:Smoke$ = perm$[smoke%]
if EVAL(ex14$)
if EVAL(ex13$)
if EVAL(ex16$)
if EVAL(ex8$)
whileloop 120:animal%=&Loop:Animal$ = perm$[animal%]
IF EVAL(ex3$)
IF EVAL(ex7$)
IF EVAL(ex11$)
IF EVAL(ex12$)
font 2
PRINT "\n\n House Drink Nation Colour Smoke Animal\n"
Whileloop 5:house%=&Loop
Tab( 3):PRINT house%,
Tab(10):PRINT Drink$[ord(MID$(Drink$,house%,1))-65],
Tab(22):PRINT Nation$[ord(MID$(Nation$,house%,1))-65],
Tab(34):PRINT Colr$[ord(MID$(Colr$,house%,1))-65],
Tab(46):PRINT Smoke$[ord(MID$(Smoke$,house%,1))-65],
Tab(58):PRINT Animal$[ord(MID$(Animal$,house%,1))-65]
EndWhile : INC solutions%
ENDIF
ENDIF
ENDIF
ENDIF
ENDWHILE 'ANIMAL%
ENDIF
ENDIF
ENDIF
ENDIF
ENDWHILE 'SMOKE%
ENDIF
ENDIF
ENDIF
ENDWHILE ' DRINK%
ENDIF
ENDIF
ENDIF
ENDWHILE 'COLR%
ENDIF
ENDWHILE 'NATION%
PRINT "\n Nbr. of solutions = "; solutions%
PRINT " Done in ";format$("#0.00",(&getTickCount-tm&)/1000);" seconds."
BEEP
WAITINPUT
END
PROC EVAL :PARAMETERS X$
DECLARE RESULT&
EXECUTE "RESULT& = ("+X$+")"
RETURN RESULT&
ENDPROC
PROC FNPERM
PARAMETERS T$[]
DECLARE T$,K&,N&,J&,U&,V&
N&=SIZEOF(T$[])
CASE N&<2:RETURN 1
K&=N&-2:WHILE T$[K&]>=T$[K&+1]:DEC K&:CASE K&<0:BREAK:ENDWHILE
IF K&<0:CLEAR T$[]:RETURN 0:ENDIF
J&=N&-1:WHILE T$[J&]<=T$[K&]:DEC J&:ENDWHILE
T$=T$[K&]:T$[K&]=T$[J&]:T$[J&]=T$:U&=K&+1:V&=N&-1
WHILE U&<V&:T$=T$[U&]:T$[U&]=T$[V&]:T$[V&]=T$:INC U&:DEC V&:ENDWHILE
RETURN 1
ENDPROC
'Output:
'
'House Drink Nation Colour Smoke Animal
'1 Water Norway Yellow Dunhill Cats
'2 Tea Denmark Blue Blend Horse
'3 Milk England Red PallMall Birds
'4 Coffee Germany Green Prince Zebra
'5 Beer Sweden White BlueMasterDog
'
'Number of solutions = 1
'Done in 2.536 seconds.
Alles anzeigen