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

no edit summary
(4-rings or 4-squares puzzle in Chipmunk Basic)
imported>Chinhouse
No edit summary
 
(6 intermediate revisions by 5 users not shown)
Line 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,608 ⟶ 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 4,038 ⟶ 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,249 ⟶ 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 6,938 ⟶ 7,217:
{{trans|C}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var a = 0
Line 7,193 ⟶ 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