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

Source code in the factor programming language

USING: assocs interpolate io kernel math math.combinatorics
math.ranges math.parser multiline pair-rocket sequences
sequences.generalizations ;

STRING: diagram
    ${}   ${}
   /|\ /|\
  / | X | \
 /  |/ \|  \
${} - ${} - ${} - ${}
 \  |\ /|  /
  \ | X | /
   \|/ \|/
    ${}   ${}
;

CONSTANT: adjacency
H{
    0 => { 2 3 4 }
    1 => { 3 4 5 }
    2 => { 0 3 6 }
    3 => { 0 1 2 4 6 7 }
    4 => { 0 1 3 5 6 7 }
    5 => { 1 4 7 }
    6 => { 2 3 4 }
    7 => { 3 4 5 }
}

: any-consecutive? ( seq n -- ? ) [ - abs 1 = ] curry any? ;

: neighbors ( elt seq i -- seq elt )
    adjacency at swap nths swap ;

: solution? ( permutation-seq -- ? )
    dup [ neighbors any-consecutive? ] with find-index nip not ;
    
: find-solution ( -- seq )
    8 [1,b] [ solution? ] find-permutation ;
    
: display-solution ( seq -- )
    [ number>string ] map 8 firstn diagram interpolate>string
    print ;
    
: main ( -- ) find-solution display-solution ;

MAIN: main


  

You may also check:How to resolve the algorithm Sum of squares step by step in the PHP programming language
You may also check:How to resolve the algorithm Arbitrary-precision integers (included) step by step in the Scala programming language
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the Julia programming language
You may also check:How to resolve the algorithm Fork step by step in the D programming language
You may also check:How to resolve the algorithm De Polignac numbers step by step in the Quackery programming language