-
-
Save Scoutski/84e25ee8c50a8d4817ec to your computer and use it in GitHub Desktop.
Crypto Square Solution
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
class Crypto | |
def initialize phrase | |
@phrase = phrase | |
end | |
def normalize_plaintext | |
@phrase.gsub(/[^0-9a-zA-Z]/, "").downcase | |
end | |
def size | |
Math.sqrt(normalize_plaintext.length).ceil | |
end | |
def plaintext_segments | |
normalize_plaintext.scan(/.{1,#{size}}/) | |
end | |
def ciphertext | |
cipher_text = [] | |
plaintext_segments.each do |segment| | |
segment.chars.each_with_index do |letter, i| | |
cipher_text[i] ||= [] | |
cipher_text[i] << letter | |
end | |
end | |
cipher_text.join | |
end | |
def normalize_ciphertext | |
ciphertext.scan(/.{1,5}/).join(" ") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment