Nonoblock: Difference between revisions
Content added Content deleted
(Added C# implementation) |
(Added Go) |
||
Line 682: | Line 682: | ||
Possibilities: |
Possibilities: |
||
%RuntimeError{message: "Those blocks will not fit in those cells"} |
%RuntimeError{message: "Those blocks will not fit in those cells"} |
||
</pre> |
|||
=={{header|Go}}== |
|||
{{trans|Kotlin}} |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"strings" |
|||
) |
|||
func printBlock(data string, le int) { |
|||
a := []byte(data) |
|||
sumBytes := 0 |
|||
for _, b := range a { |
|||
sumBytes += int(b - 48) |
|||
} |
|||
fmt.Printf("\nblocks %c, cells %d\n", a, le) |
|||
if le-sumBytes <= 0 { |
|||
fmt.Println("No solution") |
|||
return |
|||
} |
|||
prep := make([]string, len(a)) |
|||
for i, b := range a { |
|||
prep[i] = strings.Repeat("1", int(b-48)) |
|||
} |
|||
for _, r := range genSequence(prep, le-sumBytes+1) { |
|||
fmt.Println(r[1:]) |
|||
} |
|||
} |
|||
func genSequence(ones []string, numZeros int) []string { |
|||
if len(ones) == 0 { |
|||
return []string{strings.Repeat("0", numZeros)} |
|||
} |
|||
var result []string |
|||
for x := 1; x < numZeros-len(ones)+2; x++ { |
|||
skipOne := ones[1:] |
|||
for _, tail := range genSequence(skipOne, numZeros-x) { |
|||
result = append(result, strings.Repeat("0", x)+ones[0]+tail) |
|||
} |
|||
} |
|||
return result |
|||
} |
|||
func main() { |
|||
printBlock("21", 5) |
|||
printBlock("", 5) |
|||
printBlock("8", 10) |
|||
printBlock("2323", 15) |
|||
printBlock("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> |
||