4-rings or 4-squares puzzle: Difference between revisions

no edit summary
(Added Action!)
imported>Chinhouse
No edit summary
 
(10 intermediate revisions by 8 users not shown)
Line 1,770:
Number of solutions :2860
 
</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">
rotina(min,max,unique)
{
global totalcount := 0
global totalunique := 0
global result := "min=" min " max=" max " unique=" unique "`n`n"
max := max - min + 1
loop %max%
{
a := min + A_Index - 1
loop %max%
{
b := min + A_Index - 1
loop %max%
{
c := min + A_Index - 1
loop %max%
{
d := min + A_Index - 1
loop %max%
{
e := min + A_Index - 1
loop %max%
{
f := min + A_Index - 1
loop %max%
{
g := min + A_Index - 1
sum := a+b
if (b+c+d = sum and d+e+f = sum and f+g = sum)
{
totalcount += 1
if (unique=0)
continue
if not (a=b or a=c or a=d or a=e or a=f or a=g or b=c or b=d or b=e or b=f or b=g or c=d or c=e or c=f or c=g or d=e or d=f or d=g or e=f or e=g or f=g)
{
result .= a " " b " " c " " d " " e " " f " " g "`n"
totalunique += 1
}
}
}
}
}
}
}
}
}
}
rotina(1,7,1)
MsgBox %result% `ntotal unique = %totalunique% `ntotalcount = %totalcount%
rotina(3,9,1)
MsgBox %result% `ntotal unique = %totalunique% `ntotalcount = %totalcount%
rotina(0,9,0)
MsgBox %result% `ntotalcount = %totalcount%
ExitApp
return
</syntaxhighlight>
 
{{Output}}
<pre>
min=1 max=7 unique=1
 
3 7 2 1 5 4 6
4 5 3 1 6 2 7
4 7 1 3 2 6 5
5 6 2 3 1 7 4
6 4 1 5 2 3 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
7 3 2 5 1 4 6
total unique = 8
totalcount = 497
---------------------------
OK
---------------------------
min=3 max=9 unique=1
 
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
total unique = 4
totalcount = 180
---------------------------
OK
---------------------------
min=0 max=9 unique=0
totalcount = 2860
---------------------------
OK
---------------------------
</pre>
 
Line 1,870 ⟶ 1,967:
</pre>
 
=={{header|BefungeBASIC256}}==
<syntaxhighlight lang="vb">call four_square(1, 7, TRUE, TRUE)
call four_square(3, 9, TRUE, TRUE)
call four_square(0, 9, FALSE, FALSE)
end
 
subroutine four_square(low, high, unique, show)
total = 0
 
if show then print " a b c d e f g" + chr(10) + " ============="
 
for a = low to high
for b = low to high
if unique and b = a then continue for
t = a + b
for c = low to high
if unique then
if c = a or c = b then continue for
end if
for d = low to high
if unique then
if d = a or d = b or d = c then continue for
end if
if b + c + d = t then
for e = low to high
if unique then
if e = a or e = b or e = c or e = d then continue for
end if
for f = low to high
if unique then
if f = a or f = b or f = c or f = d or f = e then continue for
end if
if d + e + f = t then
for g = low to high
if unique then
if g = a or g = b or g = c or g = d or g = e or g = f then continue for
end if
if f + g = t then
total += 1
if show then print " ";a;" ";b;" ";c;" ";d;" ";e;" ";f;" ";g
end if
next g
end if
next f
next e
end if
next d
next c
next b
next a
 
print
if unique then
print "There are ";total;" unique solutions in [";string(low);", ";string(high);"]"
else
print "There are ";total;" non-unique solutions in [";string(low);", ";string(high);"]"
end if
print
end subroutine</syntaxhighlight>
 
=={{header|Befunge}}==
This is loosely based on the [[4-rings_or_4-squares_puzzle#C|C]] algorithm, although many of the conditions have been combined to minimize branching. There is no option to choose whether the results are displayed or not - unique solutions are always displayed, and non-unique solutions just return the solution count.
 
Line 2,229 ⟶ 2,385:
 
</pre>
 
=={{header|Chipmunk Basic}}==
{{works with|Chipmunk Basic|3.6.4}}
{{trans|Applesoft BASIC}}
<syntaxhighlight lang="qbasic">10 plo = 1 : phi = 7 : punique = true : pshow = true : gosub 50 : rem "FOURSQUARES"
20 plo = 3 : phi = 9 : punique = true : pshow = true : gosub 50 : rem "FOURSQUARES"
30 plo = 0 : phi = 9 : punique = false : pshow = false : gosub 50 : rem "FOURSQUARES"
40 end
50 lo = plo
60 hi = phi
70 unique = punique
80 show = pshow
90 s = 0 : rem SOLUTIONS
100 print
110 gosub 170 : rem "ACD"
120 print
130 print s " ";
140 if not unique then print "NON-";
150 print "UNIQUE SOLUTIONS IN " lo " TO " hi
160 return
170 for c = lo to hi
180 for d = lo to hi
190 if ( not unique) or (c <> d) then
200 a = c+d
210 if (a >= lo) and (a <= hi) and (( not unique) or ((c <> 0) and (d <> 0))) then gosub 250 : rem "GE"
220 endif
230 next d,c
240 return
250 for e = lo to hi
260 if ( not unique) or ((e <> a) and (e <> c) and (e <> d)) then
270 g = d+e
280 if (g >= lo) and (g <= hi) and (( not unique) or ((g <> a) and (g <> c) and (g <> d) and (g <> e))) then gosub 320 : rem "BF"
290 endif
300 next e
310 return
320 for f = lo to hi
330 if (( not unique) or ((f <> a) and (f <> c) and (f <> d) and (f <> g) and (f <> e))) then gosub 360
340 next f
350 return
360 b = e+f-c
370 if ((b >= lo) and (b <= hi) and (( not unique) or ((b <> a) and (b <> c) and (b <> d) and (b <> g) and (b <> e) and (b <> f)))) then
380 s = s+1
390 if (show) then print a " " b " " c " " d " " e " " f " " g
400 endif
410 return</syntaxhighlight>
{{out}}
<pre>>run
 
4 7 1 3 2 6 5
6 4 1 5 2 3 7
3 7 2 1 5 4 6
5 6 2 3 1 7 4
7 3 2 5 1 4 6
4 5 3 1 6 2 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
 
8 UNIQUE SOLUTIONS IN 1 TO 7
 
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
 
4 UNIQUE SOLUTIONS IN 3 TO 9
 
 
2860 NON-UNIQUE SOLUTIONS IN 0 TO 9</pre>
 
=={{header|Clojure}}==
Line 2,443 ⟶ 2,667:
=={{header|Delphi}}==
See [[#Pascal]]
=={{header|EasyLang}}==
{{trans|AWK}}
 
<syntaxhighlight lang=easylang>
func ok v t[] .
for h in t[]
if v = h
return 0
.
.
return 1
.
proc four lo hi uni show . .
#
subr bf
for f = lo to hi
if uni = 0 or ok f [ a c d g e ] = 1
b = e + f - c
if b >= lo and b <= hi and (uni = 0 or ok b [ a c d g e f ] = 1)
solutions += 1
if show = 1
for h in [ a b c d e f g ]
write h & " "
.
print ""
.
.
.
.
.
subr ge
for e = lo to hi
if uni = 0 or ok e [ a c d ] = 1
g = d + e
if g >= lo and g <= hi and (uni = 0 or ok g [ a c d e ] = 1)
bf
.
.
.
.
subr acd
for c = lo to hi
for d = lo to hi
if uni = 0 or c <> d
a = c + d
if a >= lo and a <= hi and (uni = 0 or c <> 0 and d <> 0)
ge
.
.
.
.
.
print "low:" & lo & " hi:" & hi & " unique:" & uni
acd
print solutions & " solutions"
print ""
.
four 1 7 1 1
four 3 9 1 1
four 0 9 0 0
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
Line 3,873 ⟶ 4,159:
Total unique solutions for HIGH 9, LOW 3: 4
Total solutions for HIGH 9, LOW 0: 2860
</pre>
 
=={{header|Koka}}==
{{trans|Rust}}
<syntaxhighlight lang="koka">
fun is_unique(a: int, b: int, c: int, d: int, e: int, f: int, g: int)
a != b && a != c && a != d && a != e && a != f && a != g &&
b != c && b != d && b != e && b != f && b != g &&
c != d && c != e && c != f && c != g &&
d != e && d != f && d != g &&
e != f && e != g &&
f != g
 
fun is_solution(a: int, b: int, c: int, d: int, e: int, f: int, g: int)
val bcd = b + c + d
val ab = a + b
if ab != bcd then return False
val def = d + e + f
if bcd != def then return False
val fg = f + g
return def == fg
 
fun four_squares(low: int, high: int, unique:bool=True)
var count := 0
for(low, high) fn(a)
for(low, high) fn(b)
for(low, high) fn(c)
for(low, high) fn(d)
for(low, high) fn(e)
for(low, high) fn(f)
for(low, high) fn(g)
if (!unique || is_unique(a, b, c, d, e, f, g)) && is_solution(a, b, c, d, e, f, g) then
count := count + 1
if unique then
println([a, b, c, d, e, f, g].show)
else
()
val uniquestr = if unique then "unique" else "non-unique"
println(count.show ++ " " ++ uniquestr ++ " solutions in " ++ low.show ++ " to " ++ high.show ++ " range\n")
 
fun main()
four_squares(1, 7)
four_squares(3, 9)
four_squares(0, 9, False)
</syntaxhighlight>
 
{{out}}
<pre>
[3,7,2,1,5,4,6]
[4,5,3,1,6,2,7]
[4,7,1,3,2,6,5]
[5,6,2,3,1,7,4]
[6,4,1,5,2,3,7]
[6,4,5,1,2,7,3]
[7,2,6,1,3,5,4]
[7,3,2,5,1,4,6]
8 unique solutions in 1 to 7 range
 
[7,8,3,4,5,6,9]
[8,7,3,5,4,6,9]
[9,6,4,5,3,7,8]
[9,6,5,4,3,8,7]
4 unique solutions in 3 to 9 range
 
2860 non-unique solutions in 0 to 9 range
</pre>
 
Line 4,084 ⟶ 4,435:
 
2860</pre>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">combinations = function(elements, comboLength, unique=true)
n = elements.len
if comboLength > n then return []
allCombos = []
genCombos=function(start, currCombo)
if currCombo.len == comboLength then
allCombos.push(currCombo)
return
end if
if start == n then return
for i in range(start, n - 1)
newCombo = currCombo + [elements[i]]
genCombos(i + unique, newCombo)
end for
end function
genCombos(0, [])
return allCombos
end function
 
permutations = function(elements, permLength=null)
n = elements.len
elements.sort
if permLength == null then permLength = n
allPerms = []
genPerms = function(prefix, remainingElements)
if prefix.len == permLength then
allPerms.push(prefix)
return
end if
for i in range(0, remainingElements.len - 1)
if i > 0 and remainingElements[i] == remainingElements[i-1] then continue
newPrefix = prefix + [remainingElements[i]]
newRemains = remainingElements[:i] + remainingElements[i+1:]
genPerms(newPrefix, newRemains)
end for
end function
genPerms([],elements)
return allPerms
end function
 
ringsEqual = function(a)
if a.len != 7 then return false
return a[0]+a[1] == a[1]+a[2]+a[3] == a[3]+a[4]+a[5] == a[5] + a[6]
end function
 
fourRings = function(lo, hi, unique, show)
rng = range(lo, hi)
combos = combinations(rng, 7, unique)
cnt = 0
for c in combos
for p in permutations(c)
if ringsEqual(p) then
cnt += 1
if show then print p.join(", ")
end if
end for
end for
uniStr = [" nonunique", " unique"]
print cnt + uniStr[unique] + " solutions for " + lo + " to " + hi
print
end function
 
fourRings(1, 7, true, true)
fourRings(3, 9, true, true)
fourRings(0, 9, false, false)
</syntaxhighlight>
{{out}}
<pre>
3, 7, 2, 1, 5, 4, 6
4, 5, 3, 1, 6, 2, 7
4, 7, 1, 3, 2, 6, 5
5, 6, 2, 3, 1, 7, 4
6, 4, 1, 5, 2, 3, 7
6, 4, 5, 1, 2, 7, 3
7, 2, 6, 1, 3, 5, 4
7, 3, 2, 5, 1, 4, 6
8 unique solutions for 1 to 7
 
7, 8, 3, 4, 5, 6, 9
8, 7, 3, 5, 4, 6, 9
9, 6, 4, 5, 3, 7, 8
9, 6, 5, 4, 3, 8, 7
4 unique solutions for 3 to 9
 
2860 nonunique solutions for 0 to 9</pre>
 
=={{header|Modula-2}}==
Line 4,239 ⟶ 4,683:
 
2860 non-unique solutions in 0 to 9 range</pre>
 
=={{header|OCaml}}==
Original version by [http://rosettacode.org/wiki/User:Vanyamil User:Vanyamil]
<syntaxhighlight lang="OCaml">
 
(* Task : 4-rings_or_4-squares_puzzle *)
 
(*
Replace a, b, c, d, e, f, and g with the decimal digits LOW ───► HIGH
such that the sum of the letters inside of each of the four large squares add up to the same sum.
 
Squares are: ab; bcd; def; fg
Solution: brute force from generating a, b, d, g from possible range
*)
 
(*** Helpers ***)
 
type assignment = {
a: int;
b: int;
c: int;
d: int;
e: int;
f: int;
g: int;
}
 
let generate ((a, b), (d, g)) =
let s = a + b in
let c = s - b - d in
let f = s - g in
let e = s - f - d in
{a; b; c; d; e; f; g}
 
let list_of_assign assign =
[assign.a; assign.b; assign.c; assign.d; assign.e; assign.f; assign.g]
 
let test unique low high assign =
let l = list_of_assign assign in
let test_el e =
e >= low && e <= high &&
(not unique || (l |> List.filter ((=) e) |> List.length) == 1)
in
List.for_all test_el l
 
let generator low high =
let single () = Seq.ints low |> Seq.take_while (fun x -> x <= high) in
let first_two = Seq.product (single ()) (single ()) in
let second_two = Seq.product (single ()) (single ()) in
let final = Seq.product first_two second_two in
Seq.map generate final
 
let print_assign a =
Printf.printf "a: %d, b: %d, c: %d, d: %d, e: %d, f: %d, g: %d\n"
a.a a.b a.c a.d a.e a.f a.g
 
(*** Actual task at hand ***)
 
let evaluate low high unique log =
let seqs = generator low high |> Seq.filter (test unique low high) in
let unique_str = if unique then "unique" else "non-unique" in
if log then Seq.iter print_assign seqs;
Printf.printf "%d %s sequences found between %d and %d\n\n" (Seq.length seqs) unique_str low high
 
(*** Output ***)
 
let () =
evaluate 1 7 true true;
evaluate 3 9 true true;
evaluate 0 9 false false
;;
 
</syntaxhighlight>
{{out}}
<pre>
a: 7, b: 2, c: 6, d: 1, e: 3, f: 5, g: 4
a: 6, b: 4, c: 5, d: 1, e: 2, f: 7, g: 3
a: 3, b: 7, c: 2, d: 1, e: 5, f: 4, g: 6
a: 4, b: 5, c: 3, d: 1, e: 6, f: 2, g: 7
a: 5, b: 6, c: 2, d: 3, e: 1, f: 7, g: 4
a: 4, b: 7, c: 1, d: 3, e: 2, f: 6, g: 5
a: 7, b: 3, c: 2, d: 5, e: 1, f: 4, g: 6
a: 6, b: 4, c: 1, d: 5, e: 2, f: 3, g: 7
8 unique sequences found between 1 and 7
 
a: 9, b: 6, c: 5, d: 4, e: 3, f: 8, g: 7
a: 9, b: 6, c: 4, d: 5, e: 3, f: 7, g: 8
a: 7, b: 8, c: 3, d: 4, e: 5, f: 6, g: 9
a: 8, b: 7, c: 3, d: 5, e: 4, f: 6, g: 9
4 unique sequences found between 3 and 9
 
2860 non-unique sequences found between 0 and 9
</pre>
 
 
=={{header|Pascal}}==
Line 6,610 ⟶ 7,148:
4 unique solutions for [5,12]</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">fn main(){
mut n, mut c := get_combs(1,7,true)
println("$n unique solutions in 1 to 7")
Line 6,679 ⟶ 7,217:
{{trans|C}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var a = 0
Line 6,934 ⟶ 7,472:
There are 4 unique solutions in [3,9]
There are 2860 non-unique solutions in [0,9]</pre>
 
=={{header|Zig}}==
{{trans|Go}}
This is a direct translation of the Go solution - the Zig implementation
having manual memory management and Zig not ignoring errors or return values.
 
<syntaxhighlight lang="zig">const std = @import("std");
const Allocator = std.mem.Allocator;
</syntaxhighlight><syntaxhighlight lang="zig">
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
 
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
const ok = gpa.deinit();
std.debug.assert(ok == .ok);
}
const allocator = gpa.allocator();
 
{
const nc = try getCombs(allocator, 1, 7, true);
defer allocator.free(nc.combinations);
try stdout.print("{d} unique solutions in 1 to 7\n", .{nc.num});
try stdout.print("{any}\n", .{nc.combinations});
}
{
const nc = try getCombs(allocator, 3, 9, true);
defer allocator.free(nc.combinations);
try stdout.print("{d} unique solutions in 3 to 9\n", .{nc.num});
try stdout.print("{any}\n", .{nc.combinations});
}
{
const nc = try getCombs(allocator, 0, 9, false);
defer allocator.free(nc.combinations);
try stdout.print("{d} non-unique solutions in 0 to 9\n", .{nc.num});
}
}
</syntaxhighlight><syntaxhighlight lang="zig">
/// Caller owns combinations slice memory.
fn getCombs(allocator: Allocator, low: u16, high: u16, unique: bool) !struct { num: usize, combinations: [][7]usize } {
var num: usize = 0;
var valid_combinations = std.ArrayList([7]usize).init(allocator);
for (low..high + 1) |a|
for (low..high + 1) |b|
for (low..high + 1) |c|
for (low..high + 1) |d|
for (low..high + 1) |e|
for (low..high + 1) |f|
for (low..high + 1) |g|
if (validComb(a, b, c, d, e, f, g))
if (!unique or try isUnique(allocator, a, b, c, d, e, f, g)) {
num += 1;
try valid_combinations.append([7]usize{ a, b, c, d, e, f, g });
};
return .{ .num = num, .combinations = try valid_combinations.toOwnedSlice() };
}
</syntaxhighlight><syntaxhighlight lang="zig">
fn isUnique(allocator: Allocator, a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, g: usize) !bool {
var data = std.AutoArrayHashMap(usize, void).init(allocator);
defer data.deinit();
try data.put(a, {});
try data.put(b, {});
try data.put(c, {});
try data.put(d, {});
try data.put(e, {});
try data.put(f, {});
try data.put(g, {});
return data.count() == 7;
}
</syntaxhighlight><syntaxhighlight lang="zig">
fn validComb(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, g: usize) bool {
const square1 = a + b;
const square2 = b + c + d;
const square3 = d + e + f;
const square4 = f + g;
return square1 == square2 and square2 == square3 and square3 == square4;
}</syntaxhighlight>
{{out}}
<pre>8 unique solutions in 1 to 7
{ { 3, 7, 2, 1, 5, 4, 6 }, { 4, 5, 3, 1, 6, 2, 7 }, { 4, 7, 1, 3, 2, 6, 5 }, { 5, 6, 2, 3, 1, 7, 4 }, { 6, 4, 1, 5, 2, 3, 7 }, { 6, 4, 5, 1, 2, 7, 3 }, { 7, 2, 6, 1, 3, 5, 4 }, { 7, 3, 2, 5, 1, 4, 6 } }
4 unique solutions in 3 to 9
{ { 7, 8, 3, 4, 5, 6, 9 }, { 8, 7, 3, 5, 4, 6, 9 }, { 9, 6, 4, 5, 3, 7, 8 }, { 9, 6, 5, 4, 3, 8, 7 } }
2860 non-unique solutions in 0 to 9</pre>
 
=={{header|zkl}}==
Anonymous user