Skip to content

Instantly share code, notes, and snippets.

@geofflane
Forked from jimweirich/abstract.md
Created April 18, 2012 00:00
Show Gist options
  • Save geofflane/2410000 to your computer and use it in GitHub Desktop.
Save geofflane/2410000 to your computer and use it in GitHub Desktop.
Berlin Clock Kata
require 'time'
class BerlinClock
ON = '*'
OFF = '.'
def initialize(time)
@time = time
end
def counts
[
@time.sec % 2,
@time.hour.divmod(5),
@time.min.divmod(5)
].flatten()
end
def format()
[
make_line(counts[0], 1),
make_line(counts[1], 4),
make_line(counts[2], 4),
make_line(counts[3], 11),
make_line(counts[4], 4),
].join("\n")
end
def print()
puts format
end
private
def make_line(active, total)
ON * active + OFF * (total - active)
end
end
BerlinClock.new(Time.parse("2011-04-16 10:11:01 A.M.")).print()
puts
BerlinClock.new(Time.parse("2011-04-16 12:00:00")).print()
puts
BerlinClock.new(Time.parse("2011-04-16 23:59:59")).print()
puts
BerlinClock.new(Time.parse("2011-04-16 01:00:01")).print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment