Skip to content

Instantly share code, notes, and snippets.

@deatheragetr
Last active December 18, 2015 09:39
Show Gist options
  • Save deatheragetr/5762812 to your computer and use it in GitHub Desktop.
Save deatheragetr/5762812 to your computer and use it in GitHub Desktop.
secret message or something
code = "fx1=ZY&iX3=ZW&hm5=ZU&dg7=ZS&ei9=ZQ&ge11=ZO&tr13=ZM&hh15=ZK&uX17=ZI&si19=ZG&tX21=ZE&Xn23=ZC&hp25=ZA&pX27=YY&dl29=YW&wo31=YU&XX33=YS&XX35=YQ&re37=YO&et39=YM&ia41=YK&wn43=YI&uo45=YG&dX47=YE&so49=YC&ec51=YA&do53=XY&sX55=XW&xr57=XU&so59=XS&aX61=XQ&ph63=XO&ni65=XM&nX67=XK&Xa69=XI&yX71=XG&aX73=XE&ei75=XC&ie0=ZZ&Xa2=ZX&Xy4=ZV&aX6=ZT&Xn8=ZR&in10=ZP&hp12=ZN&Xa14=ZL&os16=ZJ&rx18=ZH&Xs20=ZF&od22=ZD&ce24=ZB&os26=YZ&Xd28=YX&ou30=YV&nw32=YT&ai34=YR&te36=YP&er38=YN&XX40=YL&XX42=YJ&ow44=YH&ld46=YF&Xp48=YD&ph50=YB&nX52=XZ&Xt54=XX&is56=XV&Xu58=XT&hh60=XR&rt62=XP&eg64=XN&ie66=XL&gd68=XJ&mh70=XH&Xi72=XF&xf74=XD"
def split_keys_and_values(code)
key_values = code.split("&")
nested_key_values = key_values.collect do |pairs|
pairs.split("=")
end
nested_key_values
end
def hash_of_charsets array_of_code
code_hash = {}
array_of_code.each do |pair|
code_hash[pair[0]] = pair[1]
end
code_hash
end
def convert_hash_values_to_positions (code_hash)
# puts code_hash.inspect
key_array = ('A'..'ZZ').to_a.reverse
code_hash.each do |key, value|
code_hash[key] = key_array.index(value)
end
code_hash
end
def order_hash_into_array unordered_hash
code_nested_array = unordered_hash.sort_by {|key, value| value}
code_array = code_nested_array.collect {|pairs| pairs[0] }
end
def remove_numbers ordered_array
code_array = ordered_array.collect do |charset|
charset.gsub(/\d/, "")
end
code_array
end
def make_string (array)
odd_string = ""
even_string = ""
array.each do |i|
odd_string << i[0]
even_string << i[1]
end
string = "#{odd_string} #{even_string}"
end
def make_spaces string
string.gsub!("X", " ")
end
def decoder (code)
array_of_splits = split_keys_and_values(code)
hash_of_charsets = hash_of_charsets array_of_splits
unordered_hash = convert_hash_values_to_positions hash_of_charsets
ordered_array = order_hash_into_array unordered_hash
without_indices = remove_numbers ordered_array
make_string = make_string without_indices
final = make_spaces make_string
final
end
puts decoder code
#BonusQuestion
def replace_spaces(string)
string.gsub!(" ", "X")
end
# At this point I think I want to create an array of arrays with the
# the nested arrays being a pair of the first and last character
def array_of_key_pairs(string)
string_forward = string
string_backward = string.reverse
array_of_keys = []
for i in 0...string.length
array_of_keys << [string_forward[i] + string_backward[i]]
end
array_of_keys
end
#in the style of the original, at this point I will
# append an index to each key character set
def append_index(array)
counter = 0
for i in array
i[0] << counter.to_s
counter += 1
end
array
end
def hash_of_code(array)
for i in array
i[0] = "&" + i[0]
i[0] = i[0] + "="
end
array
end
def add_position_values(array_of_charsets)
key = ('A'..'ZZ').to_a.reverse
for i in array_of_charsets
i[1] = key[array_of_charsets.index(i)]
end
array_of_charsets
end
def shuffle_and_make_string(array)
array.shuffle!
array.flatten!
code = array.join('')
end
def clean_up(code_string)
code_string[0] = ""
code_string
end
def encoder(string)
#clean_up(shuffle_and_make_string(add_position_values(hash_of_code(append_index(array_of_key_pairs(replace_spaces(string)))))))
first = replace_spaces(string)
second = array_of_key_pairs(first)
third = append_index(second)
fourth = hash_of_code(third)
fifth = add_position_values(fourth)
sixth = shuffle_and_make_string(fifth)
seventh = clean_up(sixth)
end
# puts clean_up(shuffle_and_make_string(add_position_values(hash_of_code(append_index(array_of_key_pairs(replace_spaces(sample_secret))))))).inspect
# Okay let's test out the decoder and encoder together!
secret = "I See Dead People"
code = encoder(secret)
puts code
puts ''
decode = decoder code
puts decode
#Hip Hip Hoorah!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment