Created
June 27, 2016 21:04
-
-
Save dmerrick/3fecdf8586c18745d0a5d24751d694db 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
#!/usr/bin/env ruby | |
# this script takes a list of encoded words, | |
# and returns the decoded message. for example: | |
# | |
# gravity-falls-decoder.rb 4-16-19 11-23-10 | |
# | |
# | |
# we'll start by setting up some important variables | |
# | |
# this will eventually contain our decoded message | |
# (as a list of letters) | |
decoded_message = [] | |
# this just says "take encoded messages from the command line" | |
encoded_words = ARGV | |
# now we make an array of the letters a-z | |
# i.e. ['a', 'b', ..., 'z'] | |
letters = ('a'..'z').to_a | |
# and then we reverse it ("switch a and z") | |
# i.e. ['z', 'y', ..., 'a'] | |
letters.reverse! | |
# | |
# okay here's where we actually decode the message. | |
# | |
# start by looping through each encoded message | |
# (in the form "4-16-19") | |
encoded_words.each do |encoded_word| | |
# split it up by the dash, so we get a list of just the numbers | |
numbers = encoded_word.split('-') | |
# loop over each number in the message | |
numbers.each do |number| | |
# convert it to an Integer so we can do math on it | |
number = number.to_i | |
# this part is a little weird: | |
# subtract 1, because in Ruby we address the position of | |
# an element in an array starting at 0. | |
# i.e. letters[0] = 'z', letters[1] = 'y' | |
number = number - 1 | |
# rotate the alphabet by 3 letters | |
# (I arrived here by trial and error) | |
# i.e. A->D, B->E, ..., Z->C | |
number = number + 3 | |
# finally, look up the number in our reversed alphabet, | |
# and add it to the decoded message | |
decoded_message << letters[number] | |
end | |
# we just finished a word, so add a space to the decoded message | |
decoded_message << ' ' | |
end | |
# | |
# all done decoding! | |
# | |
# now combine all of the decrypted letters into a single string | |
# i.e. "fonda" instead of ['f', 'o', 'n', 'd', 'a'] | |
decoded_message = decoded_message.join | |
# print it out | |
puts decoded_message | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment