zebra puzzle - A "Building" Riddle in Prolog -
i'm trying solve riddle in prolog. riddle is: there 2 buildings, each 1 has tree apartments (apartment per floor): 1 apartment of 3 rooms,one of 4 rooms , 1 of 5 rooms.
dana,joni , noah lives in building 1. ron,gale , aron lives in building 2.
joni apartment higher dana , noah. means lives on third floor of building 1. noah , gale lives on same floor (in different buildings). ron has 1 more room aron. ron lives 1 floor above gale. highest apartment in building 2 5 rooms apartment.
i need find in floor lives.
i wrote code:
solve([dana,building1,f1,r1],[noah,building1,f2,r2],[joni,building1,f3,r3],[gale,building2,f4,r4],[ron,building2,f5,r5],[aron,building2,f6,r6] ) :- l =[[dana,building1,1,3],[dana,building1,1,4],[dana,building1,1,5],[dana,building1,2,3],[dana,building1,2,4],[dana,building1,2,5],[dana,building1,3,3],[dana,building1,3,4],[dana,building1,3,5] ,[noah,building1,1,3],[noah,building1,1,4],[noah,building1,1,5],[noah,building1,2,3],[noah,building1,2,4],[noah,building1,2,5],[noah,building1,3,3],[noah,building1,3,4],[noah,building1,3,5] ,[joni,building1,1,3],[joni,building1,1,4],[joni,building1,1,5],[joni,building1,2,3],[joni,building1,2,4],[joni,building1,2,5],[joni,building1,3,3],[joni,building1,3,4],[joni,building1,3,5] ,[gale,building1,1,3],[gale,building1,1,4],[gale,building1,1,5],[gale,building1,2,3],[gale,building1,2,4],[gale,building1,2,5],[gale,building1,3,3],[gale,building1,3,4],[gale,building1,3,5] ,[ron,building1,1,3],[ron,building1,1,4],[ron,building1,1,5],[ron,building1,2,3],[ron,building1,2,4],[ron,building1,2,5],[ron,building1,3,3],[ron,building1,3,4],[ron,building1,3,5] ,[aron,building1,1,3],[aron,building1,1,4],[aron,building1,1,5],[aron,building1,2,3],[aron,building1,2,4],[aron,building1,2,5],[aron,building1,3,3],[aron,building1,3,4],[aron,building1,3,5]], f3 > f2, f3>f1, f2 == f4, r5 == r6-1, f5 == f4+1, (f4 == 3, r4 == 5;f5 == 3, r5 == 5; f6 == 3, r6 == 5), del([dana,building1,f1,r1],l,list1), del([noah,building1,f2,r2],list1,list2), del([joni,building1,f3,r3],list2,list3), del([gale,building2,f4,r4],list3,list4), del([ron,building2,f5,r5],list4,list5), del([aron,building2,f6,r6],list5,_). del(x,l,l1) :- remove(x,l,l1).
but when execute query:
solve([dana,building1,f1,r1],[noah,building1,f2,r2],[joni,building1,f3,r3],[gale,building2,f4,r4],[ron,building2,f5,r5],[aron,building2,f6,r6]).
i get:
"error 22 : instantiation error"
anyone? don't understand wrong.
i did it. answer:
solve([dana,building1,f1,r1], [noah,building1,f2,r2], [joni,building1,f3,r3], [gale,building2,f4,r4], [ron,building2,f5,r5], [aron,building2,f6,r6]) :- assign([1,2,3],[f1,f2,f3]), assign([1,2,3],[f4,f5,f6]), assign([3,4,5],[r1,r2,r3]), assign([3,4,5],[r4,r5,r6]), f3 > f2, f3>f1, f2 =:= f4, r5 =:= r6-1, f5 =:= f4+1, ( f4 =:= 3, r4 =:= 5 ; f5 =:= 3, r5 =:= 5 ; f6 =:= 3, r6 =:= 5 ). assign(_,[]). assign(digs,[d|vars]):- del(d,digs,digs1), assign(digs1,vars). del(x,l,l1):- remove(x,l,l1).
Comments
Post a Comment