/******* Prolog: What is a Part of a Room? Given part of room labels and stay data, returns most popular parts of the room. The origin is in the north-west corner. Given the room name, the map of the room (as a list of X,Y,parts of room tuples) and the time spent in the room (as a list of X,Y,seconds tuples), returns the map of the room with totals of time spent at each room part (as X,Y,part of room, seconds tuples). partofroom( bedroom,[[0,0,""],[1,0,""],[2,0,door], [0,1,bed],[1,1,next-to-robe],[2,1,robe], [0,2,""],[1,2,""],[2,2,""]], [[2,0,1],[1,0,1],[1,1,100],[1,0,1],[2,0,1], [2,0,1],[1,0,1],[0,0,1],[0,1,3000],[0,0,1],[1,0,1],[2,0,1]]). Map2=[[0,0,[],2],[1,0,[],4],[2,0,door,4],[0,1,bed,3000],[1,1,next-to-robe,100],[2,1,robe,0],[0,2,[],0],[1,2,[],0],[2,2,[],0]] *******/ partofroom(_Room,Map1,Times1) :- sumtimes1(Times1,_Times2,Map1,[],Map2), write(Map2), !. sumtimes1(Times,Times,[],Map,Map). sumtimes1(Times1,Times2,Map1,Map2,Map3) :- Map1=[Part1|Parts], Part1=[X,Y,RoomPartName], sumtimes2(X,Y,Times1,[],Times3,0,Time), Part2=[X,Y,RoomPartName,Time], append(Map2,[Part2],Map4), sumtimes1(Times3,Times2,Parts,Map4,Map3). sumtimes2(_X,_Y,[],Times,Times,Time,Time) :- !. sumtimes2(X,Y,Times1,Times2,Times3,Time1,Time2) :- Times1 = [Time4|Times4], Time4 = [X,Y,Time3], Time5 is Time1+Time3, sumtimes2(X,Y,Times4,Times2,Times3,Time5,Time2),!. sumtimes2(X1,Y1,Times1,Times2,Times3,Time1,Time2) :- Times1 = [Time3|Times4], Time3 = [X2,Y2,_Time4], (not(X1=X2); not(Y1=Y2)), append(Times2,[Time3],Times5), sumtimes2(X1,Y1,Times4,Times5,Times3,Time1,Time2).