How to resolve the algorithm Solve the no connection puzzle step by step in the J programming language

Published on 12 May 2024 09:40 PM
#J

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

Source code in the j programming language

holes=:;:'A B C D E F G H'

connections=:".;._2]0 :0
 holes e.;:'C D E'          NB. A
 holes e.;:'D E F'          NB. B
 holes e.;:'A D G'          NB. C
 holes e.;:'A B C E G H'    NB. D
 holes e.;:'A B D F G H'    NB. E
 holes e.;:'B E H'          NB. F
 holes e.;:'C D E'          NB. G
 holes e.;:'D E F'          NB. H
)
assert (-:|:) connections NB. catch typos

pegs=: 1+(A.&i.~ !)8

attempt=: [: <./@(-.&0)@,@:| connections * -/~


box=:0 :0
        A   B
       /|\ /|\
      / | X | \
     /  |/ \|  \
    C - D - E - F
     \  |\ /|  /
      \ | X | /
       \|/ \|/
        G   H
)

disp=:verb define
  rplc&(,holes;&":&>y) box
)


   (#~ 1<attempt"1) pegs
3 4 7 1 8 2 5 6
3 5 7 1 8 2 4 6
3 6 7 1 8 2 4 5
3 6 7 1 8 2 5 4
4 3 2 8 1 7 6 5
4 5 2 8 1 7 6 3
4 5 7 1 8 2 3 6
4 6 7 1 8 2 3 5
5 3 2 8 1 7 6 4
5 4 2 8 1 7 6 3
5 4 7 1 8 2 3 6
5 6 7 1 8 2 3 4
6 3 2 8 1 7 4 5
6 3 2 8 1 7 5 4
6 4 2 8 1 7 5 3
6 5 2 8 1 7 4 3


   disp {. (#~ 1<attempt"1) pegs
        3   4
       /|\ /|\
      / | X | \
     /  |/ \|  \
    7 - 1 - 8 - 2
     \  |\ /|  /
      \ | X | /
       \|/ \|/
        5   6


   (#~ 1<attempt"1) pegs
3 5 7 1 8 2 4 6
4 6 7 1 8 2 3 5
5 3 2 8 1 7 6 4
6 4 2 8 1 7 5 3


   disp {. (#~ 1<attempt"1) pegs
        3 - 5
       /|\ /|\
      / | X | \
     /  |/ \|  \
    7 - 1 - 8 - 2
     \  |\ /|  /
      \ | X | /
       \|/ \|/
        4 - 6


  

You may also check:How to resolve the algorithm Fractal tree step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm 9 billion names of God the integer step by step in the Clojure programming language
You may also check:How to resolve the algorithm Heronian triangles step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Wireworld step by step in the C++ programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Tailspin programming language