Skip to content

Instantly share code, notes, and snippets.

@dphase
Created March 12, 2014 14:58
Show Gist options
  • Save dphase/9508660 to your computer and use it in GitHub Desktop.
Save dphase/9508660 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# ---------------------------------------------------------------------------------
# Ruby Fun Times
# © #iMPULSE2013
#
# Given a 20x20 matrix representing a map or coordinates, plot 5000 points
# containing random values between 1-10. Implement a merge sort on the uniq values
# and split list into 5 groups, with each group representing a color for the
# heatmap coord. Finally, render the colorized heatmap to stdout.
# ---------------------------------------------------------------------------------
class String
COLORS = { :blue => 34,
:cyan => 36,
:green => 32,
:yellow => 33,
:red => 31 }
def colorize(color)
"\033[#{COLORS[color]}m#{self}\033[0m"
end
end
matrix = [ [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] ]
plots = (0..5000).map {|i| [rand(0..19), rand(0..19), rand(0..10)]}
# Plot points on map
# ---------------------------------------------------------------------------------
plots.each do |plot|
matrix[plot[0]][plot[1]] += plot[2]
end
# Implement a merge sort then sort our plots from the matrix
# ---------------------------------------------------------------------------------
def merge_sort(list)
return list unless list.count > 1
merge = -> (l, r) {
sorted = [] ; until l.empty? || r.empty?
sorted << (l[0] <= r[0] ? l.shift : r.shift)
end
sorted.concat(l).concat(r)
}
midpoint = list.count / 2
left, right = list[0, midpoint], list[midpoint, (list.count - midpoint)]
merge.call(merge_sort(left), merge_sort(right))
end
# Sort plots than partition into 5 groups for 5 colors
# ---------------------------------------------------------------------------------
map_vals = merge_sort(matrix.flatten.uniq)
map_vals = map_vals.each_slice((map_vals.count / 5.0).ceil).to_a
# Draw our map with pretty colors
# ---------------------------------------------------------------------------------
def render_map(map, uniq_vals)
colors = { 0 => :blue,
1 => :cyan,
2 => :green,
3 => :yellow,
4 => :red }
plot_char = '▒▒'
map.each do |m|
row = m.map do |point|
color = uniq_vals.index {|i| i.include?(point)}
plot_char.colorize(colors[color])
end
puts row.join
end
end
render_map(matrix, map_vals)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment