1

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!!

Will Ness
  • 70,110
  • 9
  • 98
  • 181
JoeShmoe
  • 13
  • 3
  • 2
    Your latest edit (i.e. removing the original puzzle description and code) makes the question and answers hard to understand. It's better if you reset this edit. Also, the code that's left has still the issues that where answered by @CapelliC and me. – hakank Oct 21 '19 at 10:14
  • I removed most of the unimportant code and the word for word question because I do not want a classmate simply googling the entire question, ending up here, copying my code, and us both getting in unnecessary trouble for plagiarism. I did update the prompt, hopefully that clears things up. And I did not want to update the code in the question to the correct code so that others could see the mistakes I had in case anyone else experiences something similar. Again I greatly appreciate your help here. – JoeShmoe Oct 21 '19 at 17:58
  • about the problem statement, this can't be helped: it must be clear and full. about the code though you were right to retain the original problematic code, otherwise the answers would make no sense. if you have new problems with your newer code, post a new question. :) also, SO is a dangerous tool this way: you just might get full code in an answer, and your lecturer can find it as easily as your classmate. check out [this answer of mine](https://stackoverflow.com/a/20092493/849891) on this subject, and its links. :) cheers, – Will Ness Oct 22 '19 at 17:29
  • @JoeShmoe Please be aware that, per the [terms of service](https://stackoverflow.com/legal/terms-of-service/public#licensing) (read "Subscriber Content"), the content you contribute here, even in the question, belongs to S.O. and you relinquish control over whether it is displayed. If you want hints instead of full answers because you are in school, make that part of your question statement. – Daniel Lyons Oct 23 '19 at 14:27

2 Answers2

1

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.
CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • Fantastic! thank you. I am cleary very unfamiliar with Prolog so I do appreciate that immensely. It is still not running properly and I am getting the no in response to executing layout(X). So I will have to do a bit more digging. Thank you! – JoeShmoe Oct 20 '19 at 17:19
1

Two more things:

  • You use X for two completely different things: one is for the room structure, and the other is the variable for arnies room. Suggestion: replace room(X) to room(Rooms), and also replace to Rooms in the other necessary places.

  • The not operator which you use to negate that two rooms are not adjacent should be encoded as \+, e.g. \+adjcnt(X,Z).

hakank
  • 6,629
  • 1
  • 17
  • 27
  • Thank you! I ended up just changing Arnie's variable to an A instead of an X, thank you for pointing that out, I can't believe I overlooked that. Also switched around the operators. THANK YOU!! :) – JoeShmoe Oct 21 '19 at 01:50