Skip to content

Commit

Permalink
refactored code for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
clupasq committed Mar 24, 2015
1 parent b7164b5 commit 1d27133
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions ascii-topology-count/ascii-topology-count.rb
Expand Up @@ -5,28 +5,30 @@ def ascii_topology_count(input)
max_length = input.lines.map(&:size).max
lines = input.lines.map { |l| "%-#{max_length}s"%l }

corners_in_groups = {}
# 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
corner_groups = {}

lines.size.times { |y|
max_length.times{ |x|
max_length.times { |x|
if lines[y][x] == ?+
corners_in_groups[[y, x]] = [[y, x]]
corner_groups[[y, x]] = [[y, x]]
end
}
}

k = corners_in_groups.keys
k = corner_groups.keys

combine_groups =-> c1, c2 {
g1 = corners_in_groups[c1]
g2 = corners_in_groups[c2]
g1 = corner_groups[c1]
g2 = corner_groups[c2]

g2 += g1
corners_in_groups[c1] = g2
g2.map{|x| corners_in_groups[x] = g2}
corner_groups[c1] = g2
g2.map{|x| corner_groups[x] = g2}
}

corners_in_groups.keys.product(corners_in_groups.keys).map{|c1, c2|
corner_groups.keys.product(corner_groups.keys).map{|c1, c2|
y1,x1=c1
y2,x2=c2
if y1 == y2 && x1 < x2
Expand All @@ -50,7 +52,7 @@ def ascii_topology_count(input)
end
}

corners_in_groups.values.uniq.count
corner_groups.values.uniq.count
end


Expand Down

0 comments on commit 1d27133

Please sign in to comment.