Solve the no connection puzzle

From Rosetta Code
Revision as of 16:16, 6 October 2014 by rosettacode>Gerard Schildberger (→‎{{header|REXX}}: fixed a problem, the node table [for D (@.4) and E (@.5)] was specified incorrectly in the REXX program.)
Solve the no connection puzzle is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

You are given a box with eight holes labelled A-to-H, connected by fifteen straight lines in the pattern as shown


        A   B
       /|\ /|\
      / | X | \
     /  |/ \|  \
    C - D - E - F
     \  |\ /|  /
      \ | X | /
       \|/ \|/
        G   H

You are also given eight pegs numbered 1-to-8. The idea is to place the pegs in the holes so that the (absolute) difference between any two numbers connected by any line is greater than one.

For example, in this attempt:

        4   7
       /|\ /|\
      / | X | \
     /  |/ \|  \
    8 - 1 - 6 - 2
     \  |\ /|  /
      \ | X | /
       \|/ \|/
        3   5

Note that 7 and 6 are connected and have a difference of 1 so it is not a solution.

The task is to produce and show here one solution to the puzzle.

Reference

No Connection Puzzle (Video). Realated Tasks:

Go

A simple recursive brute force solution. <lang go>package main

import ( "fmt" "strings" )

func main() { p, tests, swaps := Solution() fmt.Println(p) fmt.Println("Tested", tests, "positions and did", swaps, "swaps.") }

// Holes A=0, B=1, …, H=7 // With connections: const conn = `

      A   B
     /|\ /|\
    / | X | \
   /  |/ \|  \
  C - D - E - F
   \  |\ /|  /
    \ | X | /
     \|/ \|/
      G   H`

var connections = []struct{ a, b int }{ {0, 2}, {0, 3}, {0, 4}, // A to C,D,E {1, 3}, {1, 4}, {1, 5}, // B to D,E,F {6, 2}, {6, 3}, {6, 4}, // G to C,D,E {7, 3}, {7, 4}, {7, 5}, // H to D,E,F {2, 3}, {3, 4}, {4, 5}, // C-D, D-E, E-F }

type pegs [8]int

// Valid checks if the pegs are a valid solution. // If the absolute difference between any pair of connected pegs is // greater than one it is a valid solution. func (p *pegs) Valid() bool { for _, c := range connections { if absdiff(p[c.a], p[c.b]) <= 1 { return false } } return true }

// Solution is a simple recursive brute force solver, // it stops at the first found solution. // It returns the solution, the number of positions tested, // and the number of pegs swapped. func Solution() (p *pegs, tests, swaps int) { var recurse func(int) bool recurse = func(i int) bool { if i >= len(p)-1 { tests++ return p.Valid() } // Try each remain peg from p[i:] in p[i] for j := i; j < len(p); j++ { swaps++ p[i], p[j] = p[j], p[i] if recurse(i + 1) { return true } p[i], p[j] = p[j], p[i] } return false } p = &pegs{1, 2, 3, 4, 5, 6, 7, 8} recurse(0) return }

func (p *pegs) String() string { return strings.Map(func(r rune) rune { if 'A' <= r && r <= 'H' { return rune(p[r-'A'] + '0') } return r }, conn) }

func absdiff(a, b int) int { if a > b { return a - b } return b - a }</lang>

Output:

       3   4
      /|\ /|\
     / | X | \
    /  |/ \|  \
   7 - 1 - 8 - 2
    \  |\ /|  /
     \ | X | /
      \|/ \|/
       5   6
Tested 12094 positions and did 20782 swaps.

Python

A brute force search solution. <lang python>from __future__ import print_function from itertools import permutations

connections = ((0, 2), (0, 3), (0, 4), # A-to-H become 0-to-7 respectively.

              (1, 3), (1, 4), (1, 5),
              (6, 2), (6, 3), (6, 4),
              (7, 3), (7, 4), (7, 5),
              (2, 3), (3, 4), (4, 5))


def ok(conn, perm):

   """Connected numbers ok?"""
   this, that = conn
   return abs(perm[this] - perm[that]) != 1


def solve():

   return [perm for perm in permutations(range(1, 9))
           if all(ok(conn, perm) for conn in connections)]


if __name__ == '__main__':

   solutions = solve()
   print("A, B, C, D, E, F, G, H =", ', '.join(str(i) for i in solutions[0]))</lang>
Output:
A, B, C, D, E, F, G, H = 3, 4, 7, 1, 8, 2, 5, 6


All solutions pretty printed

Add the following code after that above: <lang python>def pp(solution):

   """Prettyprint a solution"""
   boardformat = r"""
        A   B
       /|\ /|\
      / | X | \
     /  |/ \|  \
    C - D - E - F
     \  |\ /|  /
      \ | X | /
       \|/ \|/
        G   H"""
   for letter, number in zip("ABCDEFGH", solution):
       boardformat = boardformat.replace(letter, str(number))
   print(boardformat)


if __name__ == '__main__':

   for i, s in enumerate(solutions, 1):
       print("\nSolution", i, end=)
       pp(s)</lang>
Extra output
Solution 1
         3   4
        /|\ /|\
       / | X | \
      /  |/ \|  \
     7 - 1 - 8 - 2
      \  |\ /|  /
       \ | X | /
        \|/ \|/
         5   6

Solution 2
         3   5
        /|\ /|\
       / | X | \
      /  |/ \|  \
     7 - 1 - 8 - 2
      \  |\ /|  /
       \ | X | /
        \|/ \|/
         4   6

Solution 3
         3   6
        /|\ /|\
       / | X | \
      /  |/ \|  \
     7 - 1 - 8 - 2
      \  |\ /|  /
       \ | X | /
        \|/ \|/
         4   5

Solution 4
         3   6
        /|\ /|\
       / | X | \
      /  |/ \|  \
     7 - 1 - 8 - 2
      \  |\ /|  /
       \ | X | /
        \|/ \|/
         5   4

Solution 5
         4   3
        /|\ /|\
       / | X | \
      /  |/ \|  \
     2 - 8 - 1 - 7
      \  |\ /|  /
       \ | X | /
        \|/ \|/
         6   5

Solution 6
         4   5
        /|\ /|\
       / | X | \
      /  |/ \|  \
     2 - 8 - 1 - 7
      \  |\ /|  /
       \ | X | /
        \|/ \|/
         6   3

Solution 7
         4   5
        /|\ /|\
       / | X | \
      /  |/ \|  \
     7 - 1 - 8 - 2
      \  |\ /|  /
       \ | X | /
        \|/ \|/
         3   6

Solution 8
         4   6
        /|\ /|\
       / | X | \
      /  |/ \|  \
     7 - 1 - 8 - 2
      \  |\ /|  /
       \ | X | /
        \|/ \|/
         3   5

Solution 9
         5   3
        /|\ /|\
       / | X | \
      /  |/ \|  \
     2 - 8 - 1 - 7
      \  |\ /|  /
       \ | X | /
        \|/ \|/
         6   4

Solution 10
         5   4
        /|\ /|\
       / | X | \
      /  |/ \|  \
     2 - 8 - 1 - 7
      \  |\ /|  /
       \ | X | /
        \|/ \|/
         6   3

Solution 11
         5   4
        /|\ /|\
       / | X | \
      /  |/ \|  \
     7 - 1 - 8 - 2
      \  |\ /|  /
       \ | X | /
        \|/ \|/
         3   6

Solution 12
         5   6
        /|\ /|\
       / | X | \
      /  |/ \|  \
     7 - 1 - 8 - 2
      \  |\ /|  /
       \ | X | /
        \|/ \|/
         3   4

Solution 13
         6   3
        /|\ /|\
       / | X | \
      /  |/ \|  \
     2 - 8 - 1 - 7
      \  |\ /|  /
       \ | X | /
        \|/ \|/
         4   5

Solution 14
         6   3
        /|\ /|\
       / | X | \
      /  |/ \|  \
     2 - 8 - 1 - 7
      \  |\ /|  /
       \ | X | /
        \|/ \|/
         5   4

Solution 15
         6   4
        /|\ /|\
       / | X | \
      /  |/ \|  \
     2 - 8 - 1 - 7
      \  |\ /|  /
       \ | X | /
        \|/ \|/
         5   3

Solution 16
         6   5
        /|\ /|\
       / | X | \
      /  |/ \|  \
     2 - 8 - 1 - 7
      \  |\ /|  /
       \ | X | /
        \|/ \|/
         4   3

REXX

<lang rexx>/*REXX program solves the "no-connection" puzzle (with eight pegs). */ parse arg limit . /*# solutions*/ /* ╔═══════════════════════════╗ */ if limit== then limit=1 /* ║ A B ║ */

                                      /* ║         /│\  /│\          ║ */

@. = /* ║ / │ \/ │ \ ║ */ @.1 = 'A C D E' /* ║ / │ /\ │ \ ║ */ @.2 = 'B D E F' /* ║ / │/ \│ \ ║ */ @.3 = 'C A D G' /* ║ C────D────E────F ║ */ @.4 = 'D A B C E G' /* ║ \ │\ /│ / ║ */ @.5 = 'E A B D F H' /* ║ \ │ \/ │ / ║ */ @.6 = 'F B E G' /* ║ \ │ /\ │ / ║ */ @.7 = 'G C D E' /* ║ \│/ \│/ ║ */ @.8 = 'H D E F' /* ║ G H ║ */ cnt=0 /* ╚═══════════════════════════╝ */

                  do nodes=1  while  @.nodes\==;    _=word(@.nodes,1)
                  subs=0              /* [↓]  create list of node paths*/
                             do #=1  for  words(@.nodes)-1
                             __=word(@.nodes,#+1);  if __>_  then iterate
                             subs=subs+1;           !._.subs=__
                             end  /*#*/
                  !._.0=subs          /*assign the number of node paths*/
                  end   /*nodes*/

pegs=nodes-1 /*number of pegs to be seated. */ _=' ' /*_ is used for padding output.*/

       do a=1  for pegs;         if ?('A')  then iterate
        do b=1  for pegs;        if ?('B')  then iterate
         do c=1  for pegs;       if ?('C')  then iterate
          do d=1  for pegs;      if ?('D')  then iterate
           do e=1  for pegs;     if ?('E')  then iterate
            do f=1  for pegs;    if ?('F')  then iterate
             do g=1  for pegs;   if ?('G')  then iterate
              do h=1  for pegs;  if ?('H')  then iterate
              say _ 'a='a _ 'b='||b _ 'c='c _ 'd='d _ 'e='e _ 'f='f _ 'g='g _ 'h='h
              cnt=cnt+1;   if cnt==limit  then leave a
              end   /*h*/
             end    /*g*/
            end     /*f*/
           end      /*e*/
          end       /*d*/
         end        /*c*/
        end         /*b*/
       end          /*a*/

say /*display a blank line to screen.*/ s=left('s',cnt\==1) /*handle case of plurals (or not)*/ say 'found ' cnt " solution"s'.' /*display the number of solutions*/ exit /*stick a fork in it, we're done.*/ /*──────────────────────────────────? subroutine────────────────────────*/ ?: parse arg node; nn=value(node); nL=nn-1; nH=nn+1

 do cn=c2d('A')  to c2d(node)-1; if value(d2c(cn))==nn then return 1; end
                                      /* [↑]  see if any are duplicates*/
    do ch=1  for !.node.0             /* [↓]  see if any  ¬ =  ±1 value*/
    $=!.node.ch;  fn=value($)         /*node name and its current peg#.*/
    if nL==fn | nH==fn  then return 1 /*if ≡ ±1, then it can't be used.*/
    end   /*ch*/                      /* [↑]  looking for suitable num.*/

return 0 /*the sub arg value passed is OK.*/</lang> output when using the default input:

     a=3      b=4      c=7      d=1      e=8      f=2      g=5      h=6

found  1  solution.

output when using the input of:   999

     a=3      b=4      c=7      d=1      e=8      f=2      g=5      h=6
     a=3      b=5      c=7      d=1      e=8      f=2      g=4      h=6
     a=3      b=6      c=7      d=1      e=8      f=2      g=4      h=5
     a=3      b=6      c=7      d=1      e=8      f=2      g=5      h=4
     a=4      b=3      c=2      d=8      e=1      f=7      g=6      h=5
     a=4      b=5      c=2      d=8      e=1      f=7      g=6      h=3
     a=4      b=5      c=7      d=1      e=8      f=2      g=3      h=6
     a=4      b=6      c=7      d=1      e=8      f=2      g=3      h=5
     a=5      b=3      c=2      d=8      e=1      f=7      g=6      h=4
     a=5      b=4      c=2      d=8      e=1      f=7      g=6      h=3
     a=5      b=4      c=7      d=1      e=8      f=2      g=3      h=6
     a=5      b=6      c=7      d=1      e=8      f=2      g=3      h=4
     a=6      b=3      c=2      d=8      e=1      f=7      g=4      h=5
     a=6      b=3      c=2      d=8      e=1      f=7      g=5      h=4
     a=6      b=4      c=2      d=8      e=1      f=7      g=5      h=3
     a=6      b=5      c=2      d=8      e=1      f=7      g=4      h=3

found  16  solutions.

Ruby

This example is incorrect. Please fix the code and remove this message.

Details: 5 and 6 are connected by a line for example.

Be it Golden Frogs jumping on trancendental lilly pads, or a Knight on a board, or square pegs into round holes this is essentially a Hidato Like Problem, so I use [HLPSolver]: <lang ruby>

  1. Penney's Game
  2. Nigel_Galloway
  3. October 6th., 2014

require 'HLPSolver' ADJACENT = [] A,B,C,D,E,F,G,H = [0,1],[0,2],[1,0],[1,1],[1,2],[1,3],[2,1],[2,2]

board1 = <<EOS

 . 1 0 .
 0 0 0 0 
 . 0 0 .

EOS g = HLPsolver.new(board1) g.board[A[0]][A[1]].adj = [B,G,H,F] g.board[B[0]][B[1]].adj = [A,C,G,H] g.board[C[0]][C[1]].adj = [B,D,E,F,H] g.board[D[0]][D[1]].adj = [C,F] g.board[E[0]][E[1]].adj = [C,F] g.board[F[0]][F[1]].adj = [A,C,D,E,G] g.board[G[0]][G[1]].adj = [A,B,F,H] g.board[H[0]][H[1]].adj = [A,B,C,G] g.solve </lang>

Output:
#Problem:
     1  0   
  0  0  0  0
     0  0   

Solution:
     1  2   
  5  6  8  7
     3  4