Skip to content

Instantly share code, notes, and snippets.

@asolkar
Last active August 29, 2015 14:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asolkar/c2442c997e09f532bfcb to your computer and use it in GitHub Desktop.
Save asolkar/c2442c997e09f532bfcb to your computer and use it in GitHub Desktop.
Google for Work Riddles
#
# Google for Work riddles in follow Google+ posts
#
https://plus.google.com/u/0/+GoogleforWork/posts/iGqi6N9dwY3
https://plus.google.com/u/0/+GoogleforWork/posts/3uyrWjgz9cr
https://plus.google.com/u/0/+GoogleforWork/posts/6Z8LKYhJqWd
class CaesarCrypt
attr_accessor :offset
def initialize(offset)
@offset = offset
@alphabet = ('a'..'z').to_a
end
def decrypt(st)
work(st,-@offset)
end
def encrypt(st)
work(st,@offset)
end
def work(st, off)
sol = Array.new
st.split(//).each {|ch|
sc = (@alphabet.include?(ch.downcase)) ?
@alphabet.rotate(off)[@alphabet.index(ch.downcase)] :
ch
sol.push((ch.eql?(ch.upcase)) ? sc.upcase : sc)
}
return sol.join("")
end
end
class MorseCode
def initialize
@char = ('A'..'Z').to_a + (0..9).to_a + ['.', ',', ':', '?', '\'', '-', '/', '(', '"', '@', '=']
@morse = [
'.-',
'-...',
'-.-.',
'-..',
'.',
'..-.',
'--.',
'....',
'..',
'.---',
'-.-',
'.-..',
'--',
'-.',
'---',
'.--.',
'--.-',
'.-.',
'...',
'-',
'..-',
'...-',
'.--',
'-..-',
'-.--',
'--..',
'-----',
'.----',
'..---',
'...--',
'....-',
'.....',
'-....',
'--...',
'---..',
'----.',
'.-.-.-',
'--..--',
'---...',
'..--..',
'.----.',
'-....-',
'-..-.',
'-.--.-',
'.-..-.',
'.--.-.',
'-...-']
end
def encode(c)
sol = Array.new
c.split(//).each {|ch|
sol.push(@morse[@char.index(ch)])
}
return sol.join(" ")
end
def decode(c)
sol = Array.new
c.split(/\s+/).each {|ch|
sol.push(@char[@morse.index(ch)])
}
return sol.join("")
end
end
#
# First riddle (https://plus.google.com/u/0/+GoogleforWork/posts/iGqi6N9dwY3)
#
r = "Wuhdw brxu sdvvzrug olnh brxu wrrwkeuxvk. Grq'w ohw dqbergb hovh xvh lw, dqg jhw d qhz rqh hyhub vla prqwkv. Foliirug Vwroo"
puts "Riddle 1: #{r}"
puts " " + CaesarCrypt.new(3).decrypt(r)
#
# Second riddle (https://plus.google.com/u/0/+GoogleforWork/posts/3uyrWjgz9cr)
#
r = "bddpvout ejggfsfou gps qbttxpse b sfvtf opu Ep"
# (0..26).to_a.each {|o|
# puts "#{o} -> " + CaesarCrypt.new(o).decrypt(r).split(/\s/).reverse.join(" ")
# }
puts "Riddle 2: #{r}"
puts " " + CaesarCrypt.new(1).decrypt(r).split(/\s/).reverse.join(" ")
#
# Third riddle (https://plus.google.com/u/0/+GoogleforWork/posts/6Z8LKYhJqWd)
#
r = "-.- . -.-- ... . -.-. ..- .-. .. - -.-- .- ..- ... . .- -. -.. .- ..- - .... . -. - .. -.-. .- - .. --- -. ... - . .--. - .-- --- .- -.-. - .. ...- .- - ."
puts "Riddle 3: #{r}"
puts " " + MorseCode.new().decode(r)
# puts " " + MorseCode.new().encode(MorseCode.new().decode(r))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment