Skip to content

Instantly share code, notes, and snippets.

@evincarofautumn
Last active December 5, 2018 03:36
Show Gist options
  • Save evincarofautumn/d9bab949743bc4fc5de59555c05a3d42 to your computer and use it in GitHub Desktop.
Save evincarofautumn/d9bab949743bc4fc5de59555c05a3d42 to your computer and use it in GitHub Desktop.
Logic Puzzle
% Using integer constraint programming
:- use_module(library(clpfd)).
% Encoding the solution as a predicate
solution(Pairs, BirdPendant, WarMedal, Diamond, Ring, Vs) :-
% List the variables to solve
Table = [People, Colors, Places, Drinks, Heirlooms],
Colors = [Purple, Red, Green, White, Blue],
Places = [Dabovka, Karnaca, Baleton, Dunwall, Fraeport],
People = [Winslow, Marcolla, Contee, Natsiou, Finch],
PeopleNames = [winslow, marcolla, contee, natsiou, finch],
Drinks = [Absinthe, Wine, Whiskey, Rum, Beer],
Heirlooms = [BirdPendant, SnuffTin, Diamond, WarMedal, Ring],
% Pair the seat numbers with the names of those sitting there
pairs_keys_values(Pairs, People, PeopleNames),
% No two people share any of the properties in the table
% (I.e., one person to a seat, distinct colours, &c.)
maplist(all_distinct, Table),
append(Table, Vs),
% Number the seats 1-5
Vs ins 1..5,
% Encode all the constraints
Contee #= Purple,
Natsiou #= 1,
Red #= 2,
White #= Green + 1,
Green #= Absinthe,
Dabovka #= Blue,
next_to(BirdPendant, Dabovka),
Marcolla #= SnuffTin,
Karnaca #= Diamond,
next_to(Ring, Baleton),
next_to(Baleton, Wine),
Finch #= Whiskey,
Dunwall #= Rum,
Beer #= 3,
Winslow #= Fraeport.
% Two people are adjacent if the difference in their seat numbers is 1
next_to(H, N) :- abs(H-N) #= 1.
% Aaand solve:
%
% ?- solution(Pairs, BirdPendant, WarMedal, Diamond, Ring, Vs), label(Vs).
%
% Pairs = [4-winslow, 5-marcolla, 3-contee, 1-natsiou, 2-finch],
% BirdPendant = 2,
% WarMedal = 4,
% Diamond = 3,
% Ring = 1,
% Vs = [4, 5, 3, 1, 2, 3, 2, 4, 5|...].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment