require 'minitest/autorun'
F=->i{l=i.lines.to_a
g={}
l.size.times{|y|i.size.times{|x|l[y][x]==?+&&g[[y,x]]=[[y,x]]}}
c=->a,b{w=g[b]+g[a];w.map{|x|g[x]=w}}
k=g.keys
k.product(k).map{|n,o|
r,p=n
s,q=o
((r==s&&p<q&&l[r][p...q]=~/^\+-[|-]*$/)||(p==q&&r<s&&l[r...s].map{|l|l[p]||c}.join=~/^\+\|[|-]*$/))&&c[n,o]}
g.values.uniq.size}
describe '#f' do
def test_empty
assert_equal 0, F['']
end
def test_1
input = <<-EOS
+---+
| |
+--+ |
| |
+--+ |
| |
| |
| |
+--+ +--+
| |
+---------+
EOS
assert_equal 1, F[input]
end
def test_2
input = <<-EOS
+--+
| |
+---+ +----+ |
| | | |
+---+ +-------+
EOS
assert_equal 2, F[input]
end
def test_3
input = <<-EOS
+---------+
| +-----+ |
| | +-+ | |
| | | | | |
| | +-+ | |
| +-----+ |
+---------+
EOS
assert_equal 3, F[input]
end
def test_4
input = <<-EOS
+--------------+
| +--+ +--+ |
| | | | | |
+-|-----|-----|----+
| | | | | | | |
| +--+ +--+ +--+ |
+------------------+
+------------+
| |
+-----+ +-----+ |
| | | |
+-----|-----------+ | |
| | +--+ | | | |
+-+ +--|--|--+ +---------+
| | +-+ | | |
+------+ | | | |
+-------+ | |
|| | |
|+-----+
| |
+---+
EOS
assert_equal 4, F[input]
end
def test_5
input = <<-EOS
+--------+ +--------+ +--------+
| | | | | |
| +--|-----+ +--|-----+ |
| | | | | | | | | |
+-----|--+ +-----|--+ +--------+
| | | |
+--------+ +--------+
EOS
assert_equal 5, F[input]
end
def test_3_neaky
input = <<-EOS
+----+
+-+|+-+ |
| ||| | |
+-+|+-+ |
+----+
EOS
assert_equal 3, F[input]
end
end
cmVxdWlyZSAnbWluaXRlc3QvYXV0b3J1bicKCkY9LT5pe2w9aS5saW5lcy50b19hCmc9e30KbC5zaXplLnRpbWVze3x5fGkuc2l6ZS50aW1lc3t8eHxsW3ldW3hdPT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MKICArLS0tLS0tLS0tLS0tLS0rCiAgfCAgKy0tKyAgKy0tKyAgfAogIHwgIHwgIHwgIHwgIHwgIHwKKy18LS0tLS18LS0tLS18LS0tLSsKfCB8ICB8ICB8ICB8ICB8ICB8IHwKfCArLS0rICArLS0rICArLS0rIHwKKy0tLS0tLS0tLS0tLS0tLS0tLSsKCiAgICAgICAgICAgICAgKy0tLS0tLS0tLS0tLSsKICAgICAgICAgICAgICB8ICAgICAgICAgICAgfAogICAgICAgICstLS0tLSsgICstLS0tLSsgICB8CiAgICAgICAgfCAgICAgICAgfCAgICAgfCAgIHwKICArLS0tLS18LS0tLS0tLS0tLS0rICB8ICAgfAogIHwgICAgIHwgICstLSsgIHwgIHwgIHwgICB8CiAgKy0rICAgKy0tfC0tfC0tKyAgKy0tLS0tLS0tLSsKICAgIHwgICAgICB8ICArLSsgICAgICB8ICAgfCAgfAogICAgKy0tLS0tLSsgICAgfCAgICAgIHwgICB8ICB8CiAgICAgICAgICAgICAgICArLS0tLS0tLSsgIHwgIHwKICAgICAgICAgICAgICAgICAgICAgICB8fCAgfCAgfAogICAgICAgICAgICAgICAgICAgICAgIHwrLS0tLS0rCiAgICAgICAgICAgICAgICAgICAgICAgfCAgIHwKICAgICAgICAgICAgICAgICAgICAgICArLS0tKwpFT1MKICAgIGFzc2VydF9lcXVhbCA0LCBGW2lucHV0XQogIGVuZAoKICBkZWYgdGVzdF81CiAgICBpbnB1dCA9IDw8LUVPUworLS0tLS0tLS0rICArLS0tLS0tLS0rICArLS0tLS0tLS0rCnwgICAgICAgIHwgIHwgICAgICAgIHwgIHwgICAgICAgIHwKfCAgICAgKy0tfC0tLS0tKyAgKy0tfC0tLS0tKyAgICAgfAp8ICAgICB8ICB8ICB8ICB8ICB8ICB8ICB8ICB8ICAgICB8CistLS0tLXwtLSsgICstLS0tLXwtLSsgICstLS0tLS0tLSsKICAgICAgfCAgICAgICAgfCAgfCAgICAgICAgfAogICAgICArLS0tLS0tLS0rICArLS0tLS0tLS0rCkVPUwogICAgYXNzZXJ0X2VxdWFsIDUsIEZbaW5wdXRdCiAgZW5kCgogIGRlZiB0ZXN0XzNfbmVha3kKICAgIGlucHV0ID0gPDwtRU9TCiAgICstLS0tKworLSt8Ky0rIHwKfCB8fHwgfCB8CistK3wrLSsgfAogICArLS0tLSsKRU9TCiAgICBhc3NlcnRfZXF1YWwgMywgRltpbnB1dF0KICBlbmQKZW5kCg==