How to resolve the algorithm Solve the no connection puzzle step by step in the Prolog programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Solve the no connection puzzle step by step in the Prolog programming language
Table of Contents
Problem Statement
You are given a box with eight holes labelled A-to-H, connected by fifteen straight lines in the pattern as shown below: You are also given eight pegs numbered 1-to-8.
Place the eight pegs in the holes so that the (absolute) difference between any two numbers connected by any line is greater than one.
In this attempt: Note that 7 and 6 are connected and have a difference of 1, so it is not a solution.
Produce and show here one solution to the puzzle.
No Connection Puzzle (youtube).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Solve the no connection puzzle step by step in the Prolog programming language
Source code in the prolog programming language
:- use_module(library(clpfd)).
edge(a, c).
edge(a, d).
edge(a, e).
edge(b, d).
edge(b, e).
edge(b, f).
edge(c, d).
edge(c, g).
edge(d, e).
edge(d, g).
edge(d, h).
edge(e, f).
edge(e, g).
edge(e, h).
edge(f, h).
connected(A, B) :-
( edge(A,B); edge(B, A)).
no_connection_puzzle(Vs) :-
% construct the arranged list of the nodes
bagof(A, B^(edge(A,B); edge(B, A)), Lst),
sort(Lst, L),
length(L, Len),
% construct the list of the values
length(Vs, Len),
Vs ins 1..Len,
all_distinct(Vs),
% two connected nodes must have values different for more than 1
set_constraints(L, Vs),
label(Vs).
set_constraints([], []).
set_constraints([H | T], [VH | VT]) :-
set_constraint(H, T, VH, VT),
set_constraints(T, VT).
set_constraint(_, [], _, []).
set_constraint(H, [H1 | T1], V, [VH | VT]) :-
connected(H, H1),
( V - VH #> 1; VH - V #> 1),
set_constraint(H, T1, V, VT).
set_constraint(H, [H1 | T1], V, [_VH | VT]) :-
\+connected(H, H1),
set_constraint(H, T1, V, VT).
You may also check:How to resolve the algorithm Append a record to the end of a text file step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Guess the number/With feedback (player) step by step in the Lua programming language
You may also check:How to resolve the algorithm 4-rings or 4-squares puzzle step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Modular exponentiation step by step in the bc programming language
You may also check:How to resolve the algorithm Van der Corput sequence step by step in the ALGOL 68 programming language