Created
May 17, 2016 08:18
-
-
Save mame-n/2c61f03adbdc2601dd510966964352bd to your computer and use it in GitHub Desktop.
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 P59 | |
def initialize | |
@w_the = "the ".split("").map(&:ord) | |
end | |
def main | |
encorded_words = read_file( "cipher1.txt" ) | |
("a".ord.."z".ord).each do |key0| | |
("a".ord.."z".ord).each do |key1| | |
("a".ord.."z".ord).each do |key2| | |
decoded_words = decode( encorded_words, [key0, key1, key2] ) | |
return decoded_words.inject(:+) if real_words?( decoded_words ) | |
end | |
end | |
end | |
end | |
def real_words?( words ) | |
(0..3).any? do |shift| | |
words.drop(shift).each_slice(4).any? { |w_four| w_four == @w_the } | |
end | |
end | |
def decode( words, key ) | |
words.map.with_index do |word, idx| | |
word ^ key[idx % 3] | |
end | |
end | |
def read_file( fn ) | |
line = "" | |
open( fn ) do |fp| | |
line = fp.read.chomp | |
end | |
line.split(',').map(&:to_i) | |
end | |
end | |
if $0 == __FILE__ | |
p P59.new.main | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment