Nonoblock: Difference between revisions

3,052 bytes added ,  10 years ago
→‎Tcl: Added implementation
m (Punctuation.)
(→‎Tcl: Added implementation)
Line 359:
'Those blocks will not fit in those cells'
AssertionError: Those blocks will not fit in those cells</pre>
 
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
{{tcllib|generator}}
{{trans|Python}}
<lang tcl>package require Tcl 8.6
package require generator
 
generator define nonoblocks {blocks cells} {
set sum [tcl::mathop::+ {*}$blocks]
if {$sum == 0 || [lindex $blocks 0] == 0} {
generator yield {{0 0}}
return
} elseif {$sum + [llength $blocks] - 1 > $cells} {
error "those blocks will not fit in those cells"
}
 
set brest [lassign $blocks blen]
for {set bpos 0} {$bpos <= $cells - $sum - [llength $brest]} {incr bpos} {
if {![llength $brest]} {
generator yield [list [list $bpos $blen]]
return
}
set offset [expr {$bpos + $blen + 1}]
generator foreach subpos [nonoblocks $brest [expr {$cells - $offset}]] {
generator yield [linsert [lmap b $subpos {
lset b 0 [expr {[lindex $b 0] + $offset}]
}] 0 [list $bpos $blen]]
}
}
}
 
if {[info script] eq $::argv0} {
proc pblock {cells {vec {}}} {
set vector [lrepeat $cells "_"]
set ch 64
foreach b $vec {
incr ch
lassign $b bp bl
for {set i $bp} {$i < $bp + $bl} {incr i} {
lset vector $i [format %c $ch]
}
}
return |[join $vector "|"]|
}
proc flist {items} {
return [format "\[%s\]" [join $items ", "]]
}
foreach {blocks cells} {
{2 1} 5
{} 5
{8} 10
{2 3 2 3} 15
{2 3} 5
} {
puts "\nConfiguration:"
puts [format "%s # %d cells and %s blocks" \
[pblock $cells] $cells [flist $blocks]]
puts " Possibilities:"
set i 0
try {
generator foreach vector [nonoblocks $blocks $cells] {
puts " [pblock $cells $vector]"
incr i
}
puts " A total of $i possible configurations"
} on error msg {
puts " --> ERROR: $msg"
}
}
}
 
package provide nonoblock 1</lang>
{{out}}
<pre>
 
Configuration:
|_|_|_|_|_| # 5 cells and [2, 1] blocks
Possibilities:
|A|A|_|B|_|
|A|A|_|_|B|
|_|A|A|_|B|
A total of 3 possible configurations
 
Configuration:
|_|_|_|_|_| # 5 cells and [] blocks
Possibilities:
|_|_|_|_|_|
A total of 1 possible configurations
 
Configuration:
|_|_|_|_|_|_|_|_|_|_| # 10 cells and [8] blocks
Possibilities:
|A|A|A|A|A|A|A|A|_|_|
|_|A|A|A|A|A|A|A|A|_|
|_|_|A|A|A|A|A|A|A|A|
A total of 3 possible configurations
 
Configuration:
|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_| # 15 cells and [2, 3, 2, 3] blocks
Possibilities:
|A|A|_|B|B|B|_|C|C|_|D|D|D|_|_|
|A|A|_|B|B|B|_|C|C|_|_|D|D|D|_|
|A|A|_|B|B|B|_|C|C|_|_|_|D|D|D|
|A|A|_|B|B|B|_|_|C|C|_|D|D|D|_|
|A|A|_|B|B|B|_|_|C|C|_|_|D|D|D|
|A|A|_|B|B|B|_|_|_|C|C|_|D|D|D|
|A|A|_|_|B|B|B|_|C|C|_|D|D|D|_|
|A|A|_|_|B|B|B|_|C|C|_|_|D|D|D|
|A|A|_|_|B|B|B|_|_|C|C|_|D|D|D|
|A|A|_|_|_|B|B|B|_|C|C|_|D|D|D|
|_|A|A|_|B|B|B|_|C|C|_|D|D|D|_|
|_|A|A|_|B|B|B|_|C|C|_|_|D|D|D|
|_|A|A|_|B|B|B|_|_|C|C|_|D|D|D|
|_|A|A|_|_|B|B|B|_|C|C|_|D|D|D|
|_|_|A|A|_|B|B|B|_|C|C|_|D|D|D|
A total of 15 possible configurations
 
Configuration:
|_|_|_|_|_| # 5 cells and [2, 3] blocks
Possibilities:
--> ERROR: those blocks will not fit in those cells
</pre>
Anonymous user