Solve the no connection puzzle: Difference between revisions

m (→‎{{header|Wren}}: Minor tidy)
(3 intermediate revisions by 3 users not shown)
Line 1,126:
2 8 1 7
4 3</pre>
 
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
using System.Collections.Generic;
using System.Linq;
 
public class NoConnection
{
// adopted from Go
static int[][] links = new int[][] {
new int[] {2, 3, 4}, // A to C,D,E
new int[] {3, 4, 5}, // B to D,E,F
new int[] {2, 4}, // D to C, E
new int[] {5}, // E to F
new int[] {2, 3, 4}, // G to C,D,E
new int[] {3, 4, 5}, // H to D,E,F
};
 
static int[] pegs = new int[8];
 
public static void Main(string[] args)
{
List<int> vals = Enumerable.Range(1, 8).ToList();
Random rng = new Random();
 
do
{
vals = vals.OrderBy(a => rng.Next()).ToList();
for (int i = 0; i < pegs.Length; i++)
pegs[i] = vals[i];
 
} while (!Solved());
 
PrintResult();
}
 
static bool Solved()
{
for (int i = 0; i < links.Length; i++)
foreach (int peg in links[i])
if (Math.Abs(pegs[i] - peg) == 1)
return false;
return true;
}
 
static void PrintResult()
{
Console.WriteLine($" {pegs[0]} {pegs[1]}");
Console.WriteLine($"{pegs[2]} {pegs[3]} {pegs[4]} {pegs[5]}");
Console.WriteLine($" {pegs[6]} {pegs[7]}");
}
}
</syntaxhighlight>
{{out}}
<pre>
6 1
4 3 8 7
2 5
 
</pre>
 
=={{header|C++}}==
Line 1,969 ⟶ 2,031:
5 6
</pre>
 
=={{header|FreeBASIC}}==
{{trans|Phix}}
<syntaxhighlight lang="vbnet">Dim As String txt = "" _
" A B" & Chr(10) & _
" /|\ /|\" & Chr(10) & _
" / | X | \" & Chr(10) & _
" / |/ \| \" & Chr(10) & _
" C - D - E - F" & Chr(10) & _
" \ |\ /| /" & Chr(10) & _
" \ | X | /" & Chr(10) & _
" \|/ \|/" & Chr(10) & _
" G H"
 
Dim Shared As String links(1 To 8)
links(1) = "": links(2) = "": links(3) = "A": links(4) = "ABC"
links(5) = "ABD": links(6) = "BE": links(7) = "CDE": links(8) = "DEF"
 
Sub ReplaceString(Byref cad As String, oldChar As String, newChar As String)
Dim As Integer posic
Do
posic = Instr(cad, oldChar)
If posic = 0 Then Exit Do
cad = Left(cad, posic - 1) & newChar & Mid(cad, posic + 1)
Loop
End Sub
 
Function solve(s As String, idx As Integer, part As String) As String
Dim As Integer v, p, i, j
Dim As String res
For i = 1 To Len(s)
v = Val(Mid(s, i, 1))
For j = 1 To Len(links(idx))
p = Asc(Mid(links(idx), j, 1)) - 64
If Abs(v - Val(Mid(part, p, 1))) < 2 Then v = 0: Exit For
Next j
If v <> 0 Then
If Len(s) = 1 Then Return part + Chr(48 + v)
res = solve(Left(s, i - 1) & Mid(s, i + 1), idx + 1, part & Chr(48 + v))
If Len(res) > 0 Then Return res
End If
Next i
Return ""
End Function
 
Dim As String result = solve("12345678", 1, "")
For i As Integer = 1 To Len(result)
ReplaceString(txt, Chr(64 + i), Mid(result, i, 1))
Next i
Print txt
 
Sleep</syntaxhighlight>
{{out}}
<pre>Same as Phix entry.</pre>
 
=={{header|Go}}==
Line 4,388 ⟶ 4,506:
\|/ \|/
3 4
</pre>
 
=={{header|Zig}}==
<syntaxhighlight lang="Zig">const std = @import("std");
const print = std.debug.print;
 
const PA = 0;
const PB = 1;
const PC = 2;
const PD = 3;
const PE = 4;
const PF = 5;
const PG = 6;
const PH = 7;
 
const Pair = struct { u8, u8 };
const Pairs = [_]Pair{ .{ PA, PC }, .{ PA, PD }, .{ PA, PE }, .{ PB, PD }, .{ PB, PE }, .{ PB, PF }, .{ PC, PG }, .{ PC, PD }, .{ PD, PE }, .{ PD, PG }, .{ PD, PH }, .{ PE, PG }, .{ PE, PH }, .{ PF, PH }, .{ PE, PF } };
var t: [8]u32 = .{0} ** 8;
 
inline fn abs(p: Pair) u32 {
if (t[p.@"0"] < t[p.@"1"]) return t[p.@"1"] - t[p.@"0"];
return t[p.@"0"] - t[p.@"1"];
}
 
fn check() bool {
var r: bool = true;
for (Pairs) |p| {
r = r and abs(p) > 1;
if (!r) break;
}
 
return r;
}
 
fn has(a: u32) bool {
for (t) |v| {
if (v == a) return true;
}
return false;
}
 
fn solve(lvl: u32) bool {
if (lvl == 8) return check();
for (1..9) |v| {
if (has(v)) continue;
t[lvl] = v;
if (solve(lvl + 1)) return true;
}
t[lvl] = 0;
return false;
}
 
pub fn main() void {
_ = solve(0);
print("{{ A, B, C, D, E, F, G, H }} = {any}", .{t});
}
</syntaxhighlight>
Output:
<pre>
{ A, B, C, D, E, F, G, H } = { 3, 4, 7, 1, 8, 2, 5, 6 }
</pre>
<syntaxhighlight lang="Zig">const std = @import("std");
const print = std.debug.print;
 
const PA = 0;
const PB = 1;
const PC = 2;
const PD = 3;
const PE = 4;
const PF = 5;
const PG = 6;
const PH = 7;
 
const Pair = struct { u8, u8 };
const Pairs = [_]Pair{ .{ PA, PC }, .{ PA, PD }, .{ PA, PE }, .{ PB, PD }, .{ PB, PE }, .{ PB, PF }, .{ PC, PG }, .{ PC, PD }, .{ PD, PE }, .{ PD, PG }, .{ PD, PH }, .{ PE, PG }, .{ PE, PH }, .{ PF, PH }, .{ PE, PF } };
var t: [8]u32 = .{0} ** 8;
 
inline fn abs(p: Pair) u32 {
if (t[p.@"0"] < t[p.@"1"]) return t[p.@"1"] - t[p.@"0"];
return t[p.@"0"] - t[p.@"1"];
}
 
fn check() bool {
var r: bool = true;
for (Pairs) |p| {
r = r and abs(p) > 1;
if (!r) break;
}
 
return r;
}
 
fn has(a: u32) bool {
for (t) |v| {
if (v == a) return true;
}
return false;
}
 
fn solve(lvl: u32) bool {
if (lvl == 8) return check();
for (1..9) |v| {
if (has(v)) continue;
t[lvl] = v;
if (solve(lvl + 1)) {
print("{{ A, B, C, D, E, F, G, H }} = {any}\n", .{t});
}
}
t[lvl] = 0;
return false;
}
 
pub fn main() void {
_ = solve(0);
}
</syntaxhighlight>
Output:
<pre>
{ A, B, C, D, E, F, G, H } = { 3, 4, 7, 1, 8, 2, 5, 6 }
{ A, B, C, D, E, F, G, H } = { 3, 5, 7, 1, 8, 2, 4, 6 }
{ A, B, C, D, E, F, G, H } = { 3, 6, 7, 1, 8, 2, 4, 5 }
{ A, B, C, D, E, F, G, H } = { 3, 6, 7, 1, 8, 2, 5, 4 }
{ A, B, C, D, E, F, G, H } = { 4, 3, 2, 8, 1, 7, 6, 5 }
{ A, B, C, D, E, F, G, H } = { 4, 5, 2, 8, 1, 7, 6, 3 }
{ A, B, C, D, E, F, G, H } = { 4, 5, 7, 1, 8, 2, 3, 6 }
{ A, B, C, D, E, F, G, H } = { 4, 6, 7, 1, 8, 2, 3, 5 }
{ A, B, C, D, E, F, G, H } = { 5, 3, 2, 8, 1, 7, 6, 4 }
{ A, B, C, D, E, F, G, H } = { 5, 4, 2, 8, 1, 7, 6, 3 }
{ A, B, C, D, E, F, G, H } = { 5, 4, 7, 1, 8, 2, 3, 6 }
{ A, B, C, D, E, F, G, H } = { 5, 6, 7, 1, 8, 2, 3, 4 }
{ A, B, C, D, E, F, G, H } = { 6, 3, 2, 8, 1, 7, 4, 5 }
{ A, B, C, D, E, F, G, H } = { 6, 3, 2, 8, 1, 7, 5, 4 }
{ A, B, C, D, E, F, G, H } = { 6, 4, 2, 8, 1, 7, 5, 3 }
{ A, B, C, D, E, F, G, H } = { 6, 5, 2, 8, 1, 7, 4, 3 }
</pre>