Solve the no connection puzzle: Difference between revisions
Content added Content deleted
m (→{{header|Picat}}: Added {{out}}) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 60: | Line 60: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">V connections = [(0, 2), (0, 3), (0, 4), |
||
(1, 3), (1, 4), (1, 5), |
(1, 3), (1, 4), (1, 5), |
||
(6, 2), (6, 3), (6, 4), |
(6, 2), (6, 3), (6, 4), |
||
Line 80: | Line 80: | ||
V solutions = solve() |
V solutions = solve() |
||
print(‘A, B, C, D, E, F, G, H = ’solutions[0].join(‘, ’))</ |
print(‘A, B, C, D, E, F, G, H = ’solutions[0].join(‘, ’))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 89: | Line 89: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
This solution is a bit longer than it actually needs to be; however, it uses tasks to find the solution and the used types and solution-generating functions are well-separated, making it more amenable to other solutions or altering it to display all solutions. |
This solution is a bit longer than it actually needs to be; however, it uses tasks to find the solution and the used types and solution-generating functions are well-separated, making it more amenable to other solutions or altering it to display all solutions. |
||
<syntaxhighlight lang="ada"> |
|||
<lang Ada> |
|||
With |
With |
||
Ada.Text_IO, |
Ada.Text_IO, |
||
Line 99: | Line 99: | ||
begin |
begin |
||
Ada.Text_IO.Put_Line( Connection_Types.Image(Result) ); |
Ada.Text_IO.Put_Line( Connection_Types.Image(Result) ); |
||
end;</ |
end;</syntaxhighlight> |
||
< |
<syntaxhighlight lang="ada">Pragma Ada_2012; |
||
Package Connection_Types with Pure is |
Package Connection_Types with Pure is |
||
Line 146: | Line 146: | ||
Function Image ( Input : Partial_Board ) Return String; |
Function Image ( Input : Partial_Board ) Return String; |
||
End Connection_Types;</ |
End Connection_Types;</syntaxhighlight> |
||
<syntaxhighlight lang="ada"> |
|||
<lang Ada> |
|||
Pragma Ada_2012; |
Pragma Ada_2012; |
||
Line 154: | Line 154: | ||
Function Connection_Combinations return Partial_Board; |
Function Connection_Combinations return Partial_Board; |
||
</syntaxhighlight> |
|||
</lang> |
|||
< |
<syntaxhighlight lang="ada">Pragma Ada_2012; |
||
Package Body Connection_Types is |
Package Body Connection_Types is |
||
Line 227: | Line 227: | ||
end Image; |
end Image; |
||
End Connection_Types;</ |
End Connection_Types;</syntaxhighlight> |
||
< |
<syntaxhighlight lang="ada">Function Connection_Combinations return Partial_Board is |
||
begin |
begin |
||
Line 301: | Line 301: | ||
End return; |
End return; |
||
End Connection_Combinations; |
End Connection_Combinations; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> 4 5 |
<pre> 4 5 |
||
Line 318: | Line 318: | ||
{{works with|Dyalog APL 17.0 Unicode}} |
{{works with|Dyalog APL 17.0 Unicode}} |
||
<syntaxhighlight lang="apl"> |
|||
<lang APL> |
|||
Line 336: | Line 336: | ||
tries[''⍴solns;] |
tries[''⍴solns;] |
||
} |
} |
||
</ |
</syntaxhighlight> |
||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi}} |
{{works with|as|Raspberry Pi}} |
||
<syntaxhighlight lang="arm assembly"> |
|||
<lang ARM Assembly> |
|||
/* ARM assembly Raspberry PI */ |
/* ARM assembly Raspberry PI */ |
||
Line 879: | Line 879: | ||
iMagicNumber: .int 0xCCCCCCCD |
iMagicNumber: .int 0xCCCCCCCD |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
a=3 b=4 c=7 d=1 |
a=3 b=4 c=7 d=1 |
||
Line 897: | Line 897: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">oGrid := [[ "", "X", "X"] ; setup oGrid |
||
,[ "X", "X", "X", "X"] |
,[ "X", "X", "X", "X"] |
||
,[ "", "X", "X"]] |
,[ "", "X", "X"]] |
||
Line 969: | Line 969: | ||
list .= oGrid[row, col+1] ? row ":" col+1 "," : oGrid[row, col-1] ? row ":" col-1 "," : "" |
list .= oGrid[row, col+1] ? row ":" col+1 "," : oGrid[row, col-1] ? row ":" col-1 "," : "" |
||
return Trim(list, ",") |
return Trim(list, ",") |
||
}</ |
}</syntaxhighlight> |
||
Outputs:<pre> |
Outputs:<pre> |
||
3 5 |
3 5 |
||
Line 983: | Line 983: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="c">#include <stdbool.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
#include <math.h> |
#include <math.h> |
||
Line 1,045: | Line 1,045: | ||
solution(0, 8 - 1); |
solution(0, 8 - 1); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>----- 0 ----- |
<pre>----- 0 ----- |
||
Line 1,129: | Line 1,129: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="cpp">#include <array> |
||
#include <iostream> |
#include <iostream> |
||
#include <vector> |
#include <vector> |
||
Line 1,178: | Line 1,178: | ||
solution(0, pegs.size() - 1); |
solution(0, pegs.size() - 1); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>----- 0 ----- |
<pre>----- 0 ----- |
||
Line 1,261: | Line 1,261: | ||
=={{header|Chapel}}== |
=={{header|Chapel}}== |
||
< |
<syntaxhighlight lang="chapel">type hole = int; |
||
param A : hole = 1; |
param A : hole = 1; |
||
param B : hole = A+1; |
param B : hole = A+1; |
||
Line 1,339: | Line 1,339: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
4 5 |
4 5 |
||
Line 1,353: | Line 1,353: | ||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">void main() @safe { |
||
import std.stdio, std.math, std.algorithm, std.traits, std.string; |
import std.stdio, std.math, std.algorithm, std.traits, std.string; |
||
Line 1,379: | Line 1,379: | ||
return board.tr("ABCDEFGH", "%(%d%)".format(perm)).writeln; |
return board.tr("ABCDEFGH", "%(%d%)".format(perm)).writeln; |
||
while (perm[].nextPermutation); |
while (perm[].nextPermutation); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,396: | Line 1,396: | ||
Using a simple backtracking. |
Using a simple backtracking. |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.conv, std.string, std.typecons; |
||
// Holes A=0, B=1, ..., H=7 |
// Holes A=0, B=1, ..., H=7 |
||
Line 1,460: | Line 1,460: | ||
board.tr("ABCDEFGH", "%(%d%)".format(sol.p)).writeln; |
board.tr("ABCDEFGH", "%(%d%)".format(sol.p)).writeln; |
||
writeln("Tested ", sol.tests, " positions and did ", sol.swaps, " swaps."); |
writeln("Tested ", sol.tests, " positions and did ", sol.swaps, " swaps."); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,477: | Line 1,477: | ||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
This solution uses HLPsolver from [[Solve_a_Hidato_puzzle#Elixir | here]] |
This solution uses HLPsolver from [[Solve_a_Hidato_puzzle#Elixir | here]] |
||
< |
<syntaxhighlight lang="elixir"># It solved if connected A and B, connected G and H (according to the video). |
||
# require HLPsolver |
# require HLPsolver |
||
Line 1,502: | Line 1,502: | ||
|> Enum.zip(~w[A B C D E F G H]) |
|> Enum.zip(~w[A B C D E F G H]) |
||
|> Enum.reduce(layout, fn {n,c},acc -> String.replace(acc, c, to_string(n)) end) |
|> Enum.reduce(layout, fn {n,c},acc -> String.replace(acc, c, to_string(n)) end) |
||
|> IO.puts</ |
|> IO.puts</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,518: | Line 1,518: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: assocs interpolate io kernel math math.combinatorics |
||
math.ranges math.parser multiline pair-rocket sequences |
math.ranges math.parser multiline pair-rocket sequences |
||
sequences.generalizations ; |
sequences.generalizations ; |
||
Line 1,563: | Line 1,563: | ||
: main ( -- ) find-solution display-solution ; |
: main ( -- ) find-solution display-solution ; |
||
MAIN: main</ |
MAIN: main</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,579: | Line 1,579: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|gfortran|11.2.1}} |
{{works with|gfortran|11.2.1}} |
||
< |
<syntaxhighlight lang="fortran">! This is free and unencumbered software released into the public domain, |
||
! via the Unlicense. |
! via the Unlicense. |
||
! For more information, please refer to <http://unlicense.org/> |
! For more information, please refer to <http://unlicense.org/> |
||
Line 1,675: | Line 1,675: | ||
end subroutine print_solution |
end subroutine print_solution |
||
end program no_connection_puzzle</ |
end program no_connection_puzzle</syntaxhighlight> |
||
The first solution printed: |
The first solution printed: |
||
{{out}} |
{{out}} |
||
Line 1,692: | Line 1,692: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
A simple recursive brute force solution. |
A simple recursive brute force solution. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,781: | Line 1,781: | ||
} |
} |
||
return b - a |
return b - a |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,799: | Line 1,799: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="groovy">import java.util.stream.Collectors |
||
import java.util.stream.IntStream |
import java.util.stream.IntStream |
||
Line 1,849: | Line 1,849: | ||
println(" ${pegs[6]} ${pegs[7]}") |
println(" ${pegs[6]} ${pegs[7]}") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 6 7 |
<pre> 6 7 |
||
Line 1,856: | Line 1,856: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (permutations) |
||
solution :: [Int] |
solution :: [Int] |
||
Line 1,886: | Line 1,886: | ||
rightShift s |
rightShift s |
||
| length s > 3 = s |
| length s > 3 = s |
||
| otherwise = " " <> s</ |
| otherwise = " " <> s</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre style="font-size:80%">A = 3 |
<pre style="font-size:80%">A = 3 |
||
Line 1,905: | Line 1,905: | ||
Supporting code: |
Supporting code: |
||
< |
<syntaxhighlight lang="j">holes=:;:'A B C D E F G H' |
||
connections=:".;._2]0 :0 |
connections=:".;._2]0 :0 |
||
Line 1,938: | Line 1,938: | ||
disp=:verb define |
disp=:verb define |
||
rplc&(,holes;&":&>y) box |
rplc&(,holes;&":&>y) box |
||
)</ |
)</syntaxhighlight> |
||
Intermezzo: |
Intermezzo: |
||
< |
<syntaxhighlight lang="j"> (#~ 1<attempt"1) pegs |
||
3 4 7 1 8 2 5 6 |
3 4 7 1 8 2 5 6 |
||
3 5 7 1 8 2 4 6 |
3 5 7 1 8 2 4 6 |
||
Line 1,958: | Line 1,958: | ||
6 3 2 8 1 7 5 4 |
6 3 2 8 1 7 5 4 |
||
6 4 2 8 1 7 5 3 |
6 4 2 8 1 7 5 3 |
||
6 5 2 8 1 7 4 3</ |
6 5 2 8 1 7 4 3</syntaxhighlight> |
||
Since there's more than one arrangement where the pegs satisfy the task constraints, and since the task calls for one solution, we will need to pick one of them. We can use the "first" function to satisfy this important constraint. |
Since there's more than one arrangement where the pegs satisfy the task constraints, and since the task calls for one solution, we will need to pick one of them. We can use the "first" function to satisfy this important constraint. |
||
< |
<syntaxhighlight lang="j"> disp {. (#~ 1<attempt"1) pegs |
||
3 4 |
3 4 |
||
/|\ /|\ |
/|\ /|\ |
||
Line 1,973: | Line 1,973: | ||
5 6 |
5 6 |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Video''' |
'''Video''' |
||
Line 1,979: | Line 1,979: | ||
If we follow the video and also connect A and B as well as G and H, we get only four solutions (which we can see are reflections / rotations of each other): |
If we follow the video and also connect A and B as well as G and H, we get only four solutions (which we can see are reflections / rotations of each other): |
||
< |
<syntaxhighlight lang="j"> (#~ 1<attempt"1) pegs |
||
3 5 7 1 8 2 4 6 |
3 5 7 1 8 2 4 6 |
||
4 6 7 1 8 2 3 5 |
4 6 7 1 8 2 3 5 |
||
5 3 2 8 1 7 6 4 |
5 3 2 8 1 7 6 4 |
||
6 4 2 8 1 7 5 3</ |
6 4 2 8 1 7 5 3</syntaxhighlight> |
||
The first of these looks like this: |
The first of these looks like this: |
||
< |
<syntaxhighlight lang="j"> disp {. (#~ 1<attempt"1) pegs |
||
3 - 5 |
3 - 5 |
||
/|\ /|\ |
/|\ /|\ |
||
Line 1,997: | Line 1,997: | ||
\|/ \|/ |
\|/ \|/ |
||
4 - 6 |
4 - 6 |
||
</syntaxhighlight> |
|||
</lang> |
|||
For this puzzle, we can also see that the solution can be described as: put the starting and ending numbers in the middle - everything else follows from there. It's perhaps interesting that we get this solution even if we do not explicitly put that logic into our code - it's built into the puzzle itself and is still the only solution no matter how we arrive there. |
For this puzzle, we can also see that the solution can be described as: put the starting and ending numbers in the middle - everything else follows from there. It's perhaps interesting that we get this solution even if we do not explicitly put that logic into our code - it's built into the puzzle itself and is still the only solution no matter how we arrive there. |
||
Line 2,004: | Line 2,004: | ||
The backtracking is getting tiresome, we'll try a stochastic solution for a change.<br> |
The backtracking is getting tiresome, we'll try a stochastic solution for a change.<br> |
||
{{works with|Java|8}} |
{{works with|Java|8}} |
||
< |
<syntaxhighlight lang="java">import static java.lang.Math.abs; |
||
import java.util.*; |
import java.util.*; |
||
import static java.util.stream.Collectors.toList; |
import static java.util.stream.Collectors.toList; |
||
Line 2,049: | Line 2,049: | ||
System.out.printf(" %s %s%n", pegs[6], pegs[7]); |
System.out.printf(" %s %s%n", pegs[6], pegs[7]); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
(takes about 500 shuffles on average) |
(takes about 500 shuffles on average) |
||
<pre> 4 5 |
<pre> 4 5 |
||
Line 2,058: | Line 2,058: | ||
===ES6=== |
===ES6=== |
||
{{Trans|Haskell}} |
{{Trans|Haskell}} |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 2,268: | Line 2,268: | ||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>A = 3 |
<pre>A = 3 |
||
Line 2,294: | Line 2,294: | ||
'''Part 1: Generic functions''' |
'''Part 1: Generic functions''' |
||
< |
<syntaxhighlight lang="jq"># Short-circuit determination of whether (a|condition) |
||
# is true for all a in array: |
# is true for all a in array: |
||
def forall(array; condition): |
def forall(array; condition): |
||
Line 2,317: | Line 2,317: | ||
# Count the number of items in a stream |
# Count the number of items in a stream |
||
def count(f): reduce f as $_ (0; .+1);</ |
def count(f): reduce f as $_ (0; .+1);</syntaxhighlight> |
||
'''Part 2: The no-connections puzzle for N pegs and holes''' |
'''Part 2: The no-connections puzzle for N pegs and holes''' |
||
< |
<syntaxhighlight lang="jq"># Generate a stream of solutions. |
||
# Input should be the connections array, i.e. an array of [i,j] pairs; |
# Input should be the connections array, i.e. an array of [i,j] pairs; |
||
# N is the number of pegs and holds. |
# N is the number of pegs and holds. |
||
Line 2,332: | Line 2,332: | ||
(($p[.[0]] - $p[.[1]])|abs) != 1 ); |
(($p[.[0]] - $p[.[1]])|abs) != 1 ); |
||
. as $connections | permutations(N) | select(ok($connections);</ |
. as $connections | permutations(N) | select(ok($connections);</syntaxhighlight> |
||
'''Part 3: The 8-peg no-connection puzzle''' |
'''Part 3: The 8-peg no-connection puzzle''' |
||
< |
<syntaxhighlight lang="jq"># The connectedness matrix: |
||
# In this table, 0 represents "A", etc, and an entry [i,j] |
# In this table, 0 represents "A", etc, and an entry [i,j] |
||
# signifies that the holes with indices i and j are connected. |
# signifies that the holes with indices i and j are connected. |
||
Line 2,366: | Line 2,366: | ||
| $letters |
| $letters |
||
| reduce range(0;length) as $i ($board; index($letters[$i]) as $ix | .[$ix] = $in[$i] + 48) |
| reduce range(0;length) as $i ($board; index($letters[$i]) as $ix | .[$ix] = $in[$i] + 48) |
||
| implode;</ |
| implode;</syntaxhighlight> |
||
'''Examples''': |
'''Examples''': |
||
< |
<syntaxhighlight lang="jq"># To print all the solutions: |
||
# solve | pp |
# solve | pp |
||
Line 2,380: | Line 2,380: | ||
one(solve) | pp |
one(solve) | pp |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="sh">$ jq -n -r -f no_connection.jq |
||
5 6 |
5 6 |
||
Line 2,392: | Line 2,392: | ||
\ | X | / |
\ | X | / |
||
\|/ \|/ |
\|/ \|/ |
||
3 4</ |
3 4</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia"> |
||
using Combinatorics |
using Combinatorics |
||
Line 2,435: | Line 2,435: | ||
printsolutions() |
printsolutions() |
||
</ |
</syntaxhighlight> {{output}} <pre> |
||
Found 16 solutions. |
Found 16 solutions. |
||
3 4 |
3 4 |
||
Line 2,450: | Line 2,450: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="scala">// version 1.2.0 |
||
import kotlin.math.abs |
import kotlin.math.abs |
||
Line 2,533: | Line 2,533: | ||
println(pegsAsString(p)) |
println(pegsAsString(p)) |
||
println("Tested $tests positions and did $swaps swaps.") |
println("Tested $tests positions and did $swaps swaps.") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,554: | Line 2,554: | ||
Press space bar to see solutions so far. |
Press space bar to see solutions so far. |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
Module no_connection_puzzle { |
Module no_connection_puzzle { |
||
\\ Holes |
\\ Holes |
||
Line 2,647: | Line 2,647: | ||
no_connection_puzzle |
no_connection_puzzle |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,668: | Line 2,668: | ||
The program ought to work with any POSIX-compliant m4. The display has been changed to use only ASCII characters, because very old m4 cannot handle UTF-8. |
The program ought to work with any POSIX-compliant m4. The display has been changed to use only ASCII characters, because very old m4 cannot handle UTF-8. |
||
< |
<syntaxhighlight lang="m4">divert(-1) |
||
define(`abs',`eval(((( $1 ) < 0) * (-( $1 ))) + ((0 <= ( $1 )) * ( $1 )))') |
define(`abs',`eval(((( $1 ) < 0) * (-( $1 ))) + ((0 <= ( $1 )) * ( $1 )))') |
||
Line 2,732: | Line 2,732: | ||
divert`'dnl |
divert`'dnl |
||
dnl |
dnl |
||
solve_puzzle</ |
solve_puzzle</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,747: | Line 2,747: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
This one simply takes all permutations of the pegs and filters out invalid solutions. |
This one simply takes all permutations of the pegs and filters out invalid solutions. |
||
< |
<syntaxhighlight lang="mathematica">sol = Fold[ |
||
Select[#, |
Select[#, |
||
Function[perm, Abs[perm[[#2[[1]]]] - perm[[#2[[2]]]]] > 1]] &, |
Function[perm, Abs[perm[[#2[[1]]]] - perm[[#2[[2]]]]] > 1]] &, |
||
Line 2,757: | Line 2,757: | ||
" `` ``\n /|\\ /|\\\n / | X | \\\n / |/ \\| \\\n`` - `` \ |
" `` ``\n /|\\ /|\\\n / | X | \\\n / |/ \\| \\\n`` - `` \ |
||
- `` - ``\n \\ |\\ /| /\n \\ | X | /\n \\|/ \\|/\n `` ``", |
- `` - ``\n \\ |\\ /| /\n \\ | X | /\n \\|/ \\|/\n `` ``", |
||
Sequence @@ sol]];</ |
Sequence @@ sol]];</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 3 4 |
<pre> 3 4 |
||
Line 2,773: | Line 2,773: | ||
I choose to use one-based indexing for the array of pegs. It seems more logical here and Nim allows to choose any starting index for static arrays. |
I choose to use one-based indexing for the array of pegs. It seems more logical here and Nim allows to choose any starting index for static arrays. |
||
< |
<syntaxhighlight lang="nim">import strformat |
||
const Connections = [(1, 3), (1, 4), (1, 5), # A to C, D, E |
const Connections = [(1, 3), (1, 4), (1, 5), # A to C, D, E |
||
Line 2,818: | Line 2,818: | ||
var pegs = [Peg 1, 2, 3, 4, 5, 6, 7, 8] |
var pegs = [Peg 1, 2, 3, 4, 5, 6, 7, 8] |
||
discard pegs.findSolution(1, 8)</ |
discard pegs.findSolution(1, 8)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,902: | Line 2,902: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; |
use strict; |
||
Line 2,929: | Line 2,929: | ||
exit; |
exit; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,940: | Line 2,940: | ||
Brute force solution. I ordered the links highest letter first, then grouped by start letter to eliminate things asap. Nothing |
Brute force solution. I ordered the links highest letter first, then grouped by start letter to eliminate things asap. Nothing |
||
to eliminate when placing A and B, when placing C, check that CA>1, when placing D, check that DA,DB,DC are all >1, etc. |
to eliminate when placing A and B, when placing C, check that CA>1, when placing D, check that DA,DB,DC are all >1, etc. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">constant</span> <span style="color: #000000;">txt</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""" |
<span style="color: #008080;">constant</span> <span style="color: #000000;">txt</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""" |
||
Line 2,975: | Line 2,975: | ||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">substitute_all</span><span style="color: #0000FF;">(</span><span style="color: #000000;">txt</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ABCDEFGH"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"12345678"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)))</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">substitute_all</span><span style="color: #0000FF;">(</span><span style="color: #000000;">txt</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ABCDEFGH"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"12345678"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)))</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,990: | Line 2,990: | ||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
< |
<syntaxhighlight lang="picat">import cp. |
||
no_connection_puzzle(X) => |
no_connection_puzzle(X) => |
||
Line 3,029: | Line 3,029: | ||
" %d %d \n", |
" %d %d \n", |
||
A,B,C,D,E,F,G,H), |
A,B,C,D,E,F,G,H), |
||
println(Solution).</ |
println(Solution).</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,051: | Line 3,051: | ||
We first compute a list of nodes, with sort this list, and we attribute a value at the nodes. |
We first compute a list of nodes, with sort this list, and we attribute a value at the nodes. |
||
< |
<syntaxhighlight lang="prolog">:- use_module(library(clpfd)). |
||
edge(a, c). |
edge(a, c). |
||
Line 3,106: | Line 3,106: | ||
set_constraint(H, T1, V, VT). |
set_constraint(H, T1, V, VT). |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output : |
Output : |
||
<pre> ?- no_connection_puzzle(Vs). |
<pre> ?- no_connection_puzzle(Vs). |
||
Line 3,119: | Line 3,119: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
A brute force search solution. |
A brute force search solution. |
||
< |
<syntaxhighlight lang="python">from __future__ import print_function |
||
from itertools import permutations |
from itertools import permutations |
||
from enum import Enum |
from enum import Enum |
||
Line 3,145: | Line 3,145: | ||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
solutions = solve() |
solutions = solve() |
||
print("A, B, C, D, E, F, G, H =", ', '.join(str(i) for i in solutions[0]))</ |
print("A, B, C, D, E, F, G, H =", ', '.join(str(i) for i in solutions[0]))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,154: | Line 3,154: | ||
Add the following code after that above: |
Add the following code after that above: |
||
< |
<syntaxhighlight lang="python">def pp(solution): |
||
"""Prettyprint a solution""" |
"""Prettyprint a solution""" |
||
boardformat = r""" |
boardformat = r""" |
||
Line 3,174: | Line 3,174: | ||
for i, s in enumerate(solutions, 1): |
for i, s in enumerate(solutions, 1): |
||
print("\nSolution", i, end='') |
print("\nSolution", i, end='') |
||
pp(s)</ |
pp(s)</syntaxhighlight> |
||
;Extra output: |
;Extra output: |
||
Line 3,355: | Line 3,355: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
;; Solve the no connection puzzle. Tim Brown Oct. 2014 |
;; Solve the no connection puzzle. Tim Brown Oct. 2014 |
||
Line 3,397: | Line 3,397: | ||
"~a") pzl)) |
"~a") pzl)) |
||
(render-puzzle (find-good-network '(1 2 3 4 5 6 7 8)))</ |
(render-puzzle (find-good-network '(1 2 3 4 5 6 7 8)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,422: | Line 3,422: | ||
The idiosyncratic adjacency diagram is dealt with by the simple expedient of bending the two vertical lines <tt>||</tt> into two bows <tt>)(</tt>, such that adjacency can be calculated simply as a distance of 2 or less. |
The idiosyncratic adjacency diagram is dealt with by the simple expedient of bending the two vertical lines <tt>||</tt> into two bows <tt>)(</tt>, such that adjacency can be calculated simply as a distance of 2 or less. |
||
<lang |
<syntaxhighlight lang="raku" line>my @adjacent = gather -> $y, $x { |
||
take [$y,$x] if abs($x|$y) > 2; |
take [$y,$x] if abs($x|$y) > 2; |
||
} for flat -5 .. 5 X -5 .. 5; |
} for flat -5 .. 5 X -5 .. 5; |
||
Line 3,516: | Line 3,516: | ||
say "$tries tries"; |
say "$tries tries"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,528: | Line 3,528: | ||
=={{header|Red}}== |
=={{header|Red}}== |
||
===Basic version=== |
===Basic version=== |
||
< |
<syntaxhighlight lang="red">Red ["Solve the no connection puzzle"] |
||
points: [a b c d e f g h] |
points: [a b c d e f g h] |
||
Line 3,562: | Line 3,562: | ||
; start with and empty list |
; start with and empty list |
||
check [] |
check [] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,570: | Line 3,570: | ||
===With graphics=== |
===With graphics=== |
||
< |
<syntaxhighlight lang="red">Red [Needs: 'View] |
||
points: [a b c d e f g h] |
points: [a b c d e f g h] |
||
Line 3,617: | Line 3,617: | ||
; start with and empty list |
; start with and empty list |
||
check [] |
check [] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,624: | Line 3,624: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===unannotated solutions=== |
===unannotated solutions=== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program solves the "no─connection" puzzle (the puzzle has eight pegs). */ |
||
parse arg limit . /*number of solutions wanted.*/ /* ╔═══════════════════════════╗ */ |
parse arg limit . /*number of solutions wanted.*/ /* ╔═══════════════════════════╗ */ |
||
if limit=='' | limit=="," then limit= 1 /* ║ A B ║ */ |
if limit=='' | limit=="," then limit= 1 /* ║ A B ║ */ |
||
Line 3,680: | Line 3,680: | ||
if nL==fn | nH==fn then return 1 /*if ≡ ±1, then the node can't be used.*/ |
if nL==fn | nH==fn then return 1 /*if ≡ ±1, then the node can't be used.*/ |
||
end /*ch*/ /* [↑] looking for suitable number. */ |
end /*ch*/ /* [↑] looking for suitable number. */ |
||
return 0 /*the subroutine arg value passed is OK.*/</ |
return 0 /*the subroutine arg value passed is OK.*/</syntaxhighlight> |
||
{{out|output|text= when using the default input:}} |
{{out|output|text= when using the default input:}} |
||
<pre> |
<pre> |
||
Line 3,711: | Line 3,711: | ||
===annotated solutions=== |
===annotated solutions=== |
||
Usage note: if the '''limit''' (the 1<sup>st</sup> argument) is negative, a diagram (node graph) is shown. |
Usage note: if the '''limit''' (the 1<sup>st</sup> argument) is negative, a diagram (node graph) is shown. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program solves the "no─connection" puzzle (the puzzle has eight pegs). */ |
||
@abc= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
@abc= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
||
parse arg limit . /*number of solutions wanted.*/ /* ╔═══════════════════════════╗ */ |
parse arg limit . /*number of solutions wanted.*/ /* ╔═══════════════════════════╗ */ |
||
Line 3,788: | Line 3,788: | ||
end /*box*/ |
end /*box*/ |
||
say _ 'a='a _ "b="||b _ 'c='c _ "d="d _ ' e='e _ "f="f _ 'g='g _ "h="h |
say _ 'a='a _ "b="||b _ 'c='c _ "d="d _ ' e='e _ "f="f _ 'g='g _ "h="h |
||
return</ |
return</syntaxhighlight> |
||
{{out|output|text= when using the default inputs of: <tt> -1 </tt>}} |
{{out|output|text= when using the default inputs of: <tt> -1 </tt>}} |
||
<pre> |
<pre> |
||
Line 3,836: | Line 3,836: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
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 [http://rosettacode.org/wiki/Solve_a_Hidato_puzzle#With_Warnsdorff HLPSolver]: |
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 [http://rosettacode.org/wiki/Solve_a_Hidato_puzzle#With_Warnsdorff HLPSolver]: |
||
< |
<syntaxhighlight lang="ruby"> |
||
# Solve No Connection Puzzle |
# Solve No Connection Puzzle |
||
# |
# |
||
Line 3,861: | Line 3,861: | ||
g.board[H[0]][H[1]].adj = [A,B,C,G] |
g.board[H[0]][H[1]].adj = [A,B,C,G] |
||
g.solve |
g.solve |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,878: | Line 3,878: | ||
{{libheader|Scala sub-repositories}} |
{{libheader|Scala sub-repositories}} |
||
{{Out}}Best seen in running your browser either by [https://scalafiddle.io/sf/Ub2LEup/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/ZXGSJLFEQe21Frh4Vwp0WA Scastie (remote JVM)]. |
{{Out}}Best seen in running your browser either by [https://scalafiddle.io/sf/Ub2LEup/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/ZXGSJLFEQe21Frh4Vwp0WA Scastie (remote JVM)]. |
||
< |
<syntaxhighlight lang="scala">object NoConnection extends App { |
||
private def links = Seq( |
private def links = Seq( |
||
Line 3,901: | Line 3,901: | ||
printResult(genRandom.dropWhile(!notSolved(links, _)).head) |
printResult(genRandom.dropWhile(!notSolved(links, _)).head) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{tcllib|struct::list}} |
{{tcllib|struct::list}} |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.6 |
||
package require struct::list |
package require struct::list |
||
Line 3,949: | Line 3,949: | ||
break |
break |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 3 4 |
<pre> 3 4 |
||
Line 3,964: | Line 3,964: | ||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
{{libheader|Wren-dynamic}} |
{{libheader|Wren-dynamic}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/dynamic" for Tuple |
||
var Solution = Tuple.create("Solution", ["p", "tests", "swaps"]) |
var Solution = Tuple.create("Solution", ["p", "tests", "swaps"]) |
||
Line 4,049: | Line 4,049: | ||
var s = solve.call() |
var s = solve.call() |
||
System.print(pegsAsString.call(s.p)) |
System.print(pegsAsString.call(s.p)) |
||
System.print("Tested %(s.tests) positions and did %(s.swaps) swaps.")</ |
System.print("Tested %(s.tests) positions and did %(s.swaps) swaps.")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,067: | Line 4,067: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; |
||
int Hole, Max, I; |
int Hole, Max, I; |
||
Line 4,103: | Line 4,103: | ||
until Hole = 8; |
until Hole = 8; |
||
Text(0, Str); |
Text(0, Str); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,120: | Line 4,120: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="zkl">const PegA=0, PegB=1, PegC=2, PegD=3, PegE=4, PegF=5, PegG=6, PegH=7; |
||
connections:=T( |
connections:=T( |
||
T(PegA, PegC), T(PegA, PegD), T(PegA, PegE), |
T(PegA, PegC), T(PegA, PegD), T(PegA, PegE), |
||
Line 4,146: | Line 4,146: | ||
board.translate("ABCDEFGH",p.apply('+(1)).concat()).println(); |
board.translate("ABCDEFGH",p.apply('+(1)).concat()).println(); |
||
break; // comment out to see all 16 solutions |
break; // comment out to see all 16 solutions |
||
}</ |
}</syntaxhighlight> |
||
The filter1 method stops on the first True, so it acts like a conditional or. |
The filter1 method stops on the first True, so it acts like a conditional or. |
||
{{out}} |
{{out}} |