Nonoblock: Difference between revisions
Content added Content deleted
(Added Wren) |
|||
Line 2,320: | Line 2,320: | ||
Possibilities: |
Possibilities: |
||
--> ERROR: those blocks will not fit in those cells |
--> ERROR: those blocks will not fit in those cells |
||
</pre> |
|||
=={{header|Wren}}== |
|||
{{trans|Kotlin}} |
|||
{{libheader|Wren-math}} |
|||
<lang ecmascript>import "/math" for Nums |
|||
var genSequence // recursive |
|||
genSequence = Fn.new { |ones, numZeros| |
|||
if (ones.isEmpty) return ["0" * numZeros] |
|||
var result = [] |
|||
for (x in 1...numZeros - ones.count + 2) { |
|||
var skipOne = ones[1..-1] |
|||
for (tail in genSequence.call(skipOne, numZeros - x)) { |
|||
result.add("0" * x + ones[0] + tail) |
|||
} |
|||
} |
|||
return result |
|||
} |
|||
var printBlock = Fn.new { |data, len| |
|||
var a = data.toList |
|||
var sumChars = Nums.sum(a.map { |c| c.bytes[0] - 48 }.toList) |
|||
System.print("\nblocks %(a), cells %(len)") |
|||
if (len - sumChars <= 0) { |
|||
System.print("No solution") |
|||
return |
|||
} |
|||
var prep = a.map { |c| "1" * (c.bytes[0] - 48) }.toList |
|||
for (r in genSequence.call(prep, len - sumChars + 1)) { |
|||
System.print(r[1..-1]) |
|||
} |
|||
} |
|||
printBlock.call("21", 5) |
|||
printBlock.call("", 5) |
|||
printBlock.call("8", 10) |
|||
printBlock.call("2323", 15) |
|||
printBlock.call("23", 5)</lang> |
|||
{{out}} |
|||
<pre> |
|||
blocks [2, 1], cells 5 |
|||
11010 |
|||
11001 |
|||
01101 |
|||
blocks [], cells 5 |
|||
00000 |
|||
blocks [8], cells 10 |
|||
1111111100 |
|||
0111111110 |
|||
0011111111 |
|||
blocks [2, 3, 2, 3], cells 15 |
|||
110111011011100 |
|||
110111011001110 |
|||
110111011000111 |
|||
110111001101110 |
|||
110111001100111 |
|||
110111000110111 |
|||
110011101101110 |
|||
110011101100111 |
|||
110011100110111 |
|||
110001110110111 |
|||
011011101101110 |
|||
011011101100111 |
|||
011011100110111 |
|||
011001110110111 |
|||
001101110110111 |
|||
blocks [2, 3], cells 5 |
|||
No solution |
|||
</pre> |
</pre> |
||