4-rings or 4-squares puzzle: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Minor tidy) |
imported>Chinhouse No edit summary |
||
Line 4,435: | Line 4,435: | ||
2860</pre> |
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}}== |
=={{header|Modula-2}}== |