Created
November 6, 2020 06:49
-
-
Save havenwood/b79d26f9b052da7a103205a2e5533d13 to your computer and use it in GitHub Desktop.
Cistercian numerals (See https://en.wikipedia.org/wiki/The_Ciphers_of_the_Monks)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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