Nonoblock: Difference between revisions

Content added Content deleted
(→‎{{header|Ruby}}: add Class version)
(→‎{{header|Ruby}}: It moves a change method to the cell form to the String class.)
Line 353: Line 353:
<lang ruby>def nonoblocks(cell, blocks)
<lang ruby>def nonoblocks(cell, blocks)
raise 'Those blocks will not fit in those cells' if cell < blocks.inject(0,:+) + blocks.size - 1
raise 'Those blocks will not fit in those cells' if cell < blocks.inject(0,:+) + blocks.size - 1
nblock(cell, blocks, '', [])
result = []
nblock(cell, blocks, '', result)
result
end
end


Line 366: Line 364:
rest = cell - blocks.inject(:+) - blocks.size + 2
rest = cell - blocks.inject(:+) - blocks.size + 2
bl, brest = blocks[0], blocks.drop(1)
bl, brest = blocks[0], blocks.drop(1)
rest.times do |i|
rest.times.inject(result) do |res, i|
nblock(cell-i-bl-1, brest, position + '.'*i + '#'*bl + '.', result)
nblock(cell-i-bl-1, brest, position + '.'*i + '#'*bl + '.', res)
end
end
end
end
Line 427: Line 425:
</pre>
</pre>


'''Class version:'''
===Class version===
The output form consulted the one of the python.
<lang ruby>class NonoBlock
<lang ruby>class NonoBlock
def self.cell(cell)
"|#{(['_']*cell).join('|')}|"
end
def initialize(cell, blocks)
def initialize(cell, blocks)
raise 'Those blocks will not fit in those cells' if cell < blocks.inject(0,:+) + blocks.size - 1
raise 'Those blocks will not fit in those cells' if cell < blocks.inject(0,:+) + blocks.size - 1
Line 440: Line 435:
def result(correct=true)
def result(correct=true)
correct ? @result.map{|str| str2cell(str)} : @result
correct ? @result.map(&:nonocell) : @result
end
end
Line 457: Line 452:
end
end
end
end
end

def str2cell(str) # "##.###..##" -> "|A|A|_|B|B|B|_|_|C|C|"
class String
def nonocell # "##.###..##" -> "|A|A|_|B|B|B|_|_|C|C|"
chr = ('A'..'Z').each
chr = ('A'..'Z').each
s = str.tr('.','_').gsub(/#+/){|sharp| chr.next * sharp.size}
s = tr('.','_').gsub(/#+/){|sharp| chr.next * sharp.size}
"|#{s.chars.join('|')}|"
"|#{s.chars.join('|')}|"
end
end
Line 470: Line 467:
[10, [8]],
[10, [8]],
[15, [2, 3, 2, 3]],
[15, [2, 3, 2, 3]],
[ 5, [2, 3]], ]
[ 5, [2, 3]] ]
conf.each do |cell, blocks|
conf.each do |cell, blocks|
begin
begin
puts "Configuration:",
puts "Configuration:",
"#{NonoBlock.cell(cell)} # #{cell} cells and #{blocks} blocks",
"#{('.'*cell).nonocell} # #{cell} cells and #{blocks} blocks",
"Possibilities:"
"Possibilities:"
result = NonoBlock.new(cell, blocks).result
result = NonoBlock.new(cell, blocks).result
Line 484: Line 481:
end
end
end</lang>
end</lang>
The output form consulted the one of the python.


{{out}}
{{out}}
<pre style="height: 64ex; overflow: scroll">
<pre>
Configuration:
Configuration:
|_|_|_|_|_| # 5 cells and [2, 1] blocks
|_|_|_|_|_| # 5 cells and [2, 1] blocks