Search code examples
prologzebra-puzzle

Prolog Logic with adjacent rooms


Here is my question:

Hunter, Laura, Addiley (jack), Ramey(Sally), and Arnie(Jim) all live in the same dorm with five adjacent bedrooms. Hunter doesn’t sleep in the 5th bedroom and Laura doesn’t sleep in the first bedroom. Arnie doesn’t sleep in the first or last bedroom, and he is not in an bedroom adjacent to Addiley or Laura. Ramey sleeps in some bedroom higher than Laura’s. Who sleeps in which bedrooms? Write a Prolog program to solve this problem.

Define what adjacency is, then what the bedrooms are, and then create a layout(X) that allows you to put in all the rules.


This is the code I have so far, I have tried a few variations of this as well:

adjcnt(X,Y) :- X = (Y+1;Y-1).

rooms([ bedroom(_, 1), bedroom(_, 2), bedroom(_, 3), bedroom(_, 4), bedroom(_, 5) ]).

layout(X) :- rooms(X),
        member( bedroom(hunter, V), X),
        member( bedroom(laura,  W), X),
        member( bedroom(arnie,  X), X),
        member( bedroom(ramey,  Y), X),
        member( bedroom(addiley,Z), X),

        V \= 5,
        W \= 1,
        X \= 1,
        X \= 5,
        X \= adjcnt(X,Z),
        X \= adjcnt(X,W),
        Y @> W.

The main issue being, am I accounting for adjacent rooms correctly? and how do I implement that correctly. I continually am getting "NO" when I try to run the code. Thank you to anyone who can assist me!!


Solution

  • at first glance, you have a typo here

    adjcnt(X,Y) :- X = (Y+1;Y-1).
    

    since (=)/2 doesn't assign to X, but attempts to unify its two arguments. And so, it obviously fails. Most likely you're looking for

    adjcnt(X,Y) :- X is Y+1; X is Y-1.