Skip to content

Commit

Permalink
ascii-topology-count - golfed the solution
Browse files Browse the repository at this point in the history
  • Loading branch information
clupasq committed Mar 24, 2015
1 parent 1d27133 commit 467c99d
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 4 deletions.
113 changes: 113 additions & 0 deletions ascii-topology-count/ascii-topology-count-golfed.rb
@@ -0,0 +1,113 @@
require 'minitest/autorun'

F=->i{l = i.lines
m = l.map(&:size).max
g = {}
l.size.times{|y|m.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]||' '}.join=~/^\+\|[|-]*$/))&&c[n, o]}
g.values.uniq.count}


class TestAsciiTopologyCount < Minitest::Test
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
8 changes: 4 additions & 4 deletions ascii-topology-count/ascii-topology-count.rb
Expand Up @@ -2,8 +2,8 @@

def ascii_topology_count(input)

max_length = input.lines.map(&:size).max
lines = input.lines.map { |l| "%-#{max_length}s"%l }
lines = input.lines
max_length = lines.map(&:size).max

# hash in which the keys are corners ("+"s), represented by their [y, x] coords
# and the values are arrays of corners, representing all corners in that group
Expand All @@ -17,8 +17,8 @@ def ascii_topology_count(input)
}
}

k = corner_groups.keys

# function that combines the groups of two different corners
# into only one group
combine_groups =-> c1, c2 {
g1 = corner_groups[c1]
g2 = corner_groups[c2]
Expand Down

0 comments on commit 467c99d

Please sign in to comment.