Solving logic puzzles with a bit of logic programming

Where you can discuss, chat about events or, organise member meetups, the world or life in general including movies, tv shows and more.

Moderators: Beerkeg, Ferefire

Post Reply
unoduetre
Veteran Member
Posts: 12030
Joined: 09 Jun 2018, 14:35
Poland

Achievements

Solving logic puzzles with a bit of logic programming

Post by unoduetre » 18 Apr 2020, 19:07

I gonna solve some logic puzzles here with some logic programming (Prolog).

Because it's fun.

If you want you can suggest some logic puzzles and I will try to solve them with some simple programs.

I might fail, ha ha!

Or maybe you want to join me? (I can provide some info about the programming language, if you don't know it. It's easy to learn.)

Or you can solve them yourself without using any programming at all. :)

unoduetre
Veteran Member
Posts: 12030
Joined: 09 Jun 2018, 14:35
Poland

Achievements

Re: Solving logic puzzles with a bit of logic programming

Post by unoduetre » 18 Apr 2020, 19:12

FACTS:
1. There are 5 houses in 5 different colours.
2. In each house lives a person with a different nationality.
3. These 5 owners drink a certain beverage, smoke a certain brand of cigarette and keep a certain pet.
4. No owners have the same pet, brand of cigaratte, or drink.

CLUES:
1. The Brit lives in a red house
2. The Swede keeps a dog
3. The Dane drinks tea
4. The green house is on the left of the white house.
5. The green house owner drinks coffee.
6. The person who smokes Pall Mall keeps birds.
7. The owner of the yellow house smokes Dunhill.
8. The man living in the house right in the center drinks milk
9. The Norwegian lives in the first house.
10. The man who smokes Blend lives next to the one who keeps cats
11. The man who keeps horses lives next to the man who smokes Dunhill
12. The owner who smokes Camel drinks beer
13. The German smokes Marlborough.
14. The Norwegian lives next to the blue house
15. The man who smokes Blend has a neighbour who drinks water.

The question is, who keeps the fish?

unoduetre
Veteran Member
Posts: 12030
Joined: 09 Jun 2018, 14:35
Poland

Achievements

Re: Solving logic puzzles with a bit of logic programming

Post by unoduetre » 19 Apr 2020, 01:04

I gonna use this:

https://www.swi-prolog.org/

But it should work on any other Prolog as well.

unoduetre
Veteran Member
Posts: 12030
Joined: 09 Jun 2018, 14:35
Poland

Achievements

Re: Solving logic puzzles with a bit of logic programming

Post by unoduetre » 28 Apr 2020, 01:03

OK, I think I solved it. I'll show the code in a minute after some cleanup. :)

unoduetre
Veteran Member
Posts: 12030
Joined: 09 Jun 2018, 14:35
Poland

Achievements

Re: Solving logic puzzles with a bit of logic programming

Post by unoduetre » 28 Apr 2020, 01:07

Code: Select all

% description(HouseColour, Nationality, Beverage, Cigarette, Pet)

matchesHouseColour(HouseColour, description(HouseColour, _, _, _, _)).

matchesNationality(Nationality, description(_, Nationality, _, _, _)).

matchesBeverage(Beverage, description(_, _, Beverage, _, _)).

matchesCigarette(Cigarette, description(_, _, _, Cigarette, _)).

matchesPet(Pet, description(_, _, _, _, Pet)).

member(Element, [Element|_]).
member(Element, [_|Tail]) :- member(Element, Tail).

subset([], _).
subset([Head|Tail], List) :- member(Head, List), subset(Tail, List).

append([], List, List).
append([Head|Tail1], List, [Head|Tail2]) :- append(Tail1, List, Tail2).

sublist(List1, List2) :- append(List3, _, List2), append(_, List1, List3).

transform(_, [], []).
transform(Predicate, [Head1|Tail1], [Head2|Tail2]) :- call(Predicate, Head1, Head2), transform(Predicate, Tail1, Tail2).

matchAnyTwoSubsequentDescriptionsInAnyOrder(Description1, Description2, Solution) :-
  sublist([Description1, Description2], Solution);
  sublist([Description2, Description1], Solution).

matchesValues(Predicate, List, Solution) :- transform(Predicate, List, TransformedList), subset(TransformedList, Solution).

clue1Matches(Solution) :- member(description(red, british, _, _, _), Solution).

clue2Matches(Solution) :- member(description(_, swedish, _, _, dog), Solution).

clue3Matches(Solution) :- member(description(_, danish, tea, _, _), Solution).

clue4Matches(Solution) :- sublist([description(green, _, _, _, _), description(white, _, _, _, _)], Solution).

clue5Matches(Solution) :- member(description(green, _, coffee, _, _), Solution).

clue6Matches(Solution) :- member(description(_, _, _, pallMall, birds), Solution).

clue7Matches(Solution) :- member(description(yellow, _, _, dunhill, _), Solution).

clue8Matches([_, _, description(_, _, milk, _, _), _, _]).

clue9Matches([description(_, norwegian, _, _, _), _, _, _, _]).

clue10Matches(Solution) :- matchAnyTwoSubsequentDescriptionsInAnyOrder(description(_, _, _, blend, _), description(_, _, _, _, cats), Solution).

clue11Matches(Solution) :- matchAnyTwoSubsequentDescriptionsInAnyOrder(description(_, _, _, _, horses), description(_, _, _, dunhill, _), Solution).

clue12Matches(Solution) :- member(description(_, _, beer, camel, _), Solution).

clue13Matches(Solution) :- member(description(_, german, _, marlborough, _), Solution).

clue14Matches(Solution) :- matchAnyTwoSubsequentDescriptionsInAnyOrder(description(_, norwegian, _, _, _), description(blue, _, _, _, _), Solution).

clue15Matches(Solution) :- matchAnyTwoSubsequentDescriptionsInAnyOrder(description(_, _, _, blend, _), description(_, _, water, _, _), Solution).

fact1Matches(Solution) :- matchesValues(matchesHouseColour, [red, green, white, yellow, blue], Solution).

fact2Matches(Solution) :- matchesValues(matchesNationality, [british, swedish, danish, norwegian, german], Solution).

fact3Matches(Solution) :- matchesValues(matchesBeverage, [tea, coffee, milk, beer, water], Solution).

fact4Matches(Solution) :- matchesValues(matchesCigarette, [pallMall, dunhill, blend, camel, marlborough], Solution).

fact5Matches(Solution) :- matchesValues(matchesPet, [dog, birds, cats, horses, fish], Solution).

solution(Solution) :-
  Solution = [_, _, _, _, _],
  clue1Matches(Solution),
  clue2Matches(Solution),
  clue3Matches(Solution),
  clue4Matches(Solution),
  clue5Matches(Solution),
  clue6Matches(Solution),
  clue7Matches(Solution),
  clue8Matches(Solution),
  clue9Matches(Solution),
  clue10Matches(Solution),
  clue11Matches(Solution),
  clue12Matches(Solution),
  clue13Matches(Solution),
  clue14Matches(Solution),
  clue15Matches(Solution),
  fact1Matches(Solution),
  fact2Matches(Solution),
  fact3Matches(Solution),
  fact4Matches(Solution),
  fact5Matches(Solution).

EDIT: I've added a small improvement.

unoduetre
Veteran Member
Posts: 12030
Joined: 09 Jun 2018, 14:35
Poland

Achievements

Re: Solving logic puzzles with a bit of logic programming

Post by unoduetre » 28 Apr 2020, 01:10

Here is the way to run it and the produced solution:

Save the code in puzzle1.pl.

Run SWI prolog in the directory where the code is.

Code: Select all

% swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.26)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- [puzzle1].
true.

?- solution(Solution).
Solution = [description(yellow, norwegian, water, dunhill, cats), description(blue, danish, tea, blend, horses), description(red, british, milk, pallMall, birds), description(green, german, coffee, marlborough, fish), description(white, swedish, beer, camel, dog)] ;
false.

?-

unoduetre
Veteran Member
Posts: 12030
Joined: 09 Jun 2018, 14:35
Poland

Achievements

Re: Solving logic puzzles with a bit of logic programming

Post by unoduetre » 28 Apr 2020, 01:11

If anyone is interested I can explain how to read the code (it's easy) and how to run it.

unoduetre
Veteran Member
Posts: 12030
Joined: 09 Jun 2018, 14:35
Poland

Achievements

Re: Solving logic puzzles with a bit of logic programming

Post by unoduetre » 28 Apr 2020, 01:37

I need to find another puzzle to solve now. :)

unoduetre
Veteran Member
Posts: 12030
Joined: 09 Jun 2018, 14:35
Poland

Achievements

Re: Solving logic puzzles with a bit of logic programming

Post by unoduetre » 28 Apr 2020, 02:10

Eight married couples meet to lend one another some books. Couples have the same surname, employment and car. Each couple has a favorite color. Furthermore we know the following facts:
  1. Daniella Black and her husband work as Shop-Assistants.
  2. The book "The Seadog" was brought by a couple who drive a Fiat and love the color red.
  3. Owen and his wife Victoria like the color brown.
  4. Stan Horricks and his wife Hannah like the color white.
  5. Jenny Smith and her husband work as Warehouse Managers and they drive a Wartburg.
  6. Monica and her husband Alexander borrowed the book "Grandfather Joseph".
  7. Mathew and his wife like the color pink and brought the book "Mulatka Gabriela".
  8. Irene and her husband Oto work as Accountants.
  9. The book "We Were Five" was borrowed by a couple driving a Trabant.
  10. The Cermaks are both Ticket-Collectors who brought the book "Shed Stoat".
  11. Mr and Mrs Kuril are both Doctors who borrowed the book "Slovacko Judge".
  12. Paul and his wife like the color green.
  13. Veronica Dvorak and her husband like the color blue.
  14. Rick and his wife brought the book "Slovacko Judge" and they drive a Ziguli.
  15. One couple brought the book "Dame Commissar" and borrowed the book "Mulatka Gabriela".
  16. The couple who drive a Dacia, love the color violet.
  17. The couple who work as Teachers borrowed the book "Dame Commissar".
  18. The couple who work as Agriculturalists drive a Moskvic.
  19. Pamela and her husband drive a Renault and brought the book "Grandfather Joseph".
  20. Pamela and her husband borrowed the book that Mr and Mrs Zajac brought.
  21. Robert and his wife like the color yellow and borrowed the book "The Modern Comedy".
  22. Mr and Mrs Swain work as Shoppers.
  23. "The Modern Comedy" was brought by a couple driving a Skoda.
Is it a problem to find out everything about everyone from this information?

unoduetre
Veteran Member
Posts: 12030
Joined: 09 Jun 2018, 14:35
Poland

Achievements

Re: Solving logic puzzles with a bit of logic programming

Post by unoduetre » 28 Apr 2020, 02:12

I think it's a very similar style to the previous one, so I should be able to use the same approach. :)

unoduetre
Veteran Member
Posts: 12030
Joined: 09 Jun 2018, 14:35
Poland

Achievements

Solving logic puzzles with a bit of logic programming

Post by unoduetre » 27 May 2020, 21:22

I'm abandoning this thread and focus on anime, as this didn't get any interest.

Post Reply