Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created November 6, 2020 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/b79d26f9b052da7a103205a2e5533d13 to your computer and use it in GitHub Desktop.
Save havenwood/b79d26f9b052da7a103205a2e5533d13 to your computer and use it in GitHub Desktop.
# 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