42 lines
1.5 KiB
Text
42 lines
1.5 KiB
Text
F nonoblocks([Int] &blocks, Int cells) -> [[(Int, Int)]]
|
||
[[(Int, Int)]] r
|
||
I blocks.empty | blocks[0] == 0
|
||
r [+]= [(0, 0)]
|
||
E
|
||
assert(sum(blocks) + blocks.len - 1 <= cells, ‘Those blocks will not fit in those cells’)
|
||
V (blength, brest) = (blocks[0], blocks[1..])
|
||
V minspace4rest = sum(brest.map(b -> 1 + b))
|
||
|
||
L(bpos) 0 .. cells - minspace4rest - blength
|
||
I brest.empty
|
||
r [+]= [(bpos, blength)]
|
||
E
|
||
V offset = bpos + blength + 1
|
||
L(subpos) nonoblocks(&brest, cells - offset)
|
||
V rest = subpos.map((bp, bl) -> (@offset + bp, bl))
|
||
V vec = [(bpos, blength)] [+] rest
|
||
r [+]= vec
|
||
R r
|
||
|
||
F pblock(vec, cells)
|
||
‘Prettyprints each run of blocks with a different letter A.. for each block of filled cells’
|
||
V vector = [‘_’] * cells
|
||
L(bp_bl) vec
|
||
V ch = L.index + ‘A’.code
|
||
V (bp, bl) = bp_bl
|
||
L(i) bp .< bp + bl
|
||
vector[i] = I vector[i] == ‘_’ {Char(code' ch)} E Char(‘?’)
|
||
R ‘|’vector.join(‘|’)‘|’
|
||
|
||
L(blocks, cells) [
|
||
([2, 1], 5),
|
||
([Int](), 5),
|
||
([8], 10),
|
||
([2, 3, 2, 3], 15)
|
||
]
|
||
print("\nConfiguration:\n #. ## #. cells and #. blocks".format(pblock([(Int, Int)](), cells), cells, blocks))
|
||
print(‘ Possibilities:’)
|
||
V nb = nonoblocks(&blocks, cells)
|
||
L(vector) nb
|
||
print(‘ ’pblock(vector, cells))
|
||
print(‘ A total of #. Possible configurations.’.format(nb.len))
|