svn.filsa.org sketches

Rev

Blame | Last modification | View Log | RSS feed

chars = Array.new

#
# open file and separate characters
#___________________________________________________________

print 'Opening file ... '
begin
   File.open(ARGV[0]) {|file|
   file.each {|line|
   chars.push((line.chomp).split(''))
   }
   }
rescue => ex
   puts ex.message
end
puts 'done'


#
# set parameters
#___________________________________________________________

tag = 0
row = chars.length
col = chars[0].length
cluster = Hash.new
table = Hash.new

#
# scan horizontally
#___________________________________________________________

row.times {|i|
   puts "Horizontal scan : row(#{i})"
   col.times {|j|
   if j == 0
   tag += 1
   cluster[tag] = Array.new
   else
   if chars[i][j] == chars[i][j-1]
   else
   tag += 1
   cluster[tag] = Array.new
   end
   end
   cluster[tag] << [i, j]
   table[[i, j]] = tag
   }
}


#
# scan vertically
#___________________________________________________________

col.times {|j|
   puts "Vertical scan : col(#{j})"
   for i in 1...row
   if chars[i][j] == chars[i-1][j] then
   before = 0
   after = 0
   
   before = table[[i, j]]
   after = table[[i-1, j]]

   if before != after then
   temp = cluster[before]
   cluster.delete(before)
   
   temp.each {|t|
      cluster[after] << t
      table[t] = after
   }
   end
   end
   end
}


#
# extract max
#___________________________________________________________

print 'Extracting max ... '
max = 0
maxTags = Array.new
cluster.each{|key, value|
   if max < value.length then
   max = value.length
   maxTags = Array.new
   maxTags << key
   elsif max == value.length then
   maxTags << key
   end
}
puts 'done'


#
# count cluster
#___________________________________________________________

print 'Counting cluster ... '
count = Array.new(row, 0)
maxTags.each {|tag|
   cluster[tag].each {|pair|
   count[pair[0]] += 1
   }
}
puts 'done'


#
# make answer
#___________________________________________________________

ans = count.join("\n")
print ans