Skip to content

Instantly share code, notes, and snippets.

@mkeen
Created October 22, 2011 06:37
Show Gist options
  • Select an option

  • Save mkeen/1305715 to your computer and use it in GitHub Desktop.

Select an option

Save mkeen/1305715 to your computer and use it in GitHub Desktop.
Let's Get Digital Challenge
#!/usr/bin/env ruby
$characters = [[[0,1,1,0], [1,0,0,1], [1,0,0,1], [0,0,0,0], [1,0,0,1], [1,0,0,1], [0,1,1,0]], [[0,0,0,0], [0,0,0,1], [0,0,0,1], [0,0,0,0], [0,0,0,1], [0,0,0,1], [0,0,0,0]], [[0,1,1,0], [0,0,0,1], [0,0,0,1], [0,1,1,0], [1,0,0,0], [1,0,0,0], [0,1,1,0]], [[0,1,1,0], [0,0,0,1], [0,0,0,1], [0,1,1,0], [0,0,0,1], [0,0,0,1], [0,1,1,0]], [[0,0,0,0], [1,0,0,1], [1,0,0,1], [0,1,1,0], [0,0,0,1], [0,0,0,1], [0,0,0,0]], [[0,1,1,0], [1,0,0,0], [1,0,0,0], [0,1,1,0], [0,0,0,1], [0,0,0,1], [0,1,1,0]], [[0,1,1,0], [1,0,0,0], [1,0,0,0], [0,1,1,0], [1,0,0,1], [1,0,0,1], [0,1,1,0]], [[0,1,1,0], [0,0,0,1], [0,0,0,1], [0,0,0,0], [0,0,0,1], [0,0,0,1], [0,0,0,0]], [[0,1,1,0], [1,0,0,1], [1,0,0,1], [0,1,1,0], [1,0,0,1], [1,0,0,1], [0,1,1,0]], [[0,1,1,0], [1,0,0,1], [1,0,0,1], [0,1,1,0], [0,0,0,1], [0,0,0,1], [0,1,1,0]]]
class DigiChar
attr_accessor :integer
def initialize integer = nil
@integer = integer
end
end
class CharsContainer
def self.add_digit digit, redraw = false
@segments ||= []
@segments << DigiChar.new(digit)
draw unless !redraw
end
def self.set_digit index, value, redraw = true
@segments[index] = DigiChar.new(value)
draw unless !redraw
end
def self.draw
print "\e[2J\e[f\n\n"
7.times do |time|
seg_count = 0
@segments.each do |segment|
$characters[segment.integer][time].each do |char_column|
if char_column == 1 and ((segment.integer != 0) || (seg_count > 0))
print "@ "
else
print " "
end
end
if (time == 2 or time == 4) and (seg_count == 1) and $blink_on
print " | "
else
print " "
end
seg_count += 1
end
print "\n"
end
$stdout.flush
end
end
clock = Thread.new do
4.times do
CharsContainer.add_digit(0, true)
end
loop do
$time = Time.new.strftime("%I%M")
4.times do |time|
CharsContainer.set_digit time, Time.new.strftime("%I%M")[time].to_i
end
$blink_on = !$blink_on
CharsContainer.draw
sleep 0.5
end
end.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment