Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# frozen_string_literal: true
module Cistercian
LINE = %w[ ].freeze
CENTER_DOT = ' ■ '
GLYPHS = [
[' ',
' ',
' '],
['■■■',
' ',
' '],
[' ',
' ',
'■■■'],
['■ ',
' ■ ',
' ■'],
[' ■',
' ■ ',
'■ '],
['■■■',
' ■ ',
'■ '],
[' ■',
' ■',
' ■'],
['■■■',
' ■',
' ■'],
[' ■',
' ■',
'■■■'],
['■■■',
' ■',
'■■■']
].each(&:freeze).freeze
module_function
def cistercian(number)
raise ArgumentError, 'Impossibly low' if number.negative?
raise ArgumentError, 'Impossibly high' if number > 9999
ones, tens, hundreds, thousands = number.digits
lines = [
*upper_glyphs(ones: ones, tens: tens),
CENTER_DOT,
*lower_glyphs(hundreds: hundreds, thousands: thousands)
]
lines.join "\n"
end
def print_cistercian(number)
puts cistercian(number)
end
private_class_method def upper_glyphs(ones:, tens:)
GLYPHS.fetch(tens.to_i)
.map(&:reverse)
.zip(LINE, GLYPHS.fetch(ones.to_i))
.map(&:join)
end
private_class_method def lower_glyphs(hundreds:, thousands:)
GLYPHS.fetch(thousands.to_i)
.map(&:reverse)
.reverse
.zip(LINE, GLYPHS.fetch(hundreds.to_i).reverse)
.map(&:join)
end
end
require_relative 'cistercian'
include Cistercian
print_cistercian 5042
■■■■■
■■
■■■■
print_cistercian 9999
■■■■■■■
■■■■■■■
■■■■■■■
■■■■■■■
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment