How to resolve the algorithm Solve the no connection puzzle step by step in the Picat 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 Picat 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 Picat programming language

Source code in the picat programming language

import cp.

no_connection_puzzle(X) =>
  N = 8,
  X = new_list(N),
  X :: 1..N,
  Graph = 
    {{1,3}, {1,4}, {1,5},
     {2,4}, {2,5}, {2,6},
     {3,1}, {3,4}, {3,7},
     {4,1}, {4,2}, {4,3}, {4,5}, {4,7}, {4,8},
     {5,1}, {5,2}, {5,4}, {5,6}, {5,7}, {5,8},
     {6,2}, {6,5}, {6,8},
     {7,3}, {7,4}, {7,5},
     {8,4}, {8,5}, {8,6}},

  all_distinct(X),
  foreach(I in 1..Graph.length) 
     abs(X[Graph[I,1]]-X[Graph[I,2]]) #> 1 
  end,

  % symmetry breaking
  X[1] #< X[N],
  
  solve(X),
  println(X),
  nl,
  [A,B,C,D,E,F,G,H] = X,
  Solution = to_fstring(
  "    %d   %d       \n"++
  "   /|\\ /|\\      \n"++
  "  / | X | \\      \n"++
  " /  |/ \\|  \\    \n"++
  "%d - %d - %d - %d \n"++
  " \\  |\\ /|  /    \n"++
  "  \\ | X | /      \n"++
  "   \\|/ \\|/      \n"++
  "    %d   %d       \n",
  A,B,C,D,E,F,G,H),
  println(Solution).

  

You may also check:How to resolve the algorithm Assertions step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Semordnilap step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Old lady swallowed a fly step by step in the J programming language
You may also check:How to resolve the algorithm Identity matrix step by step in the APL programming language
You may also check:How to resolve the algorithm Terminal control/Display an extended character step by step in the Dc programming language