Skip to content

Instantly share code, notes, and snippets.

@levinotik
Created April 26, 2012 18:20
Show Gist options
  • Save levinotik/2501615 to your computer and use it in GitHub Desktop.
Save levinotik/2501615 to your computer and use it in GitHub Desktop.
Google Code Jam Solution
# this solves google code jam problem found here: http://code.google.com/codejam/contest/1460488/dashboard#s=p0
require 'pp'
#create map using the sample input and output
input = "ejp mysljylc kd kxveddknmc re jsicpdrysi
rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd
de kr kd eoya kw aej tysr re ujdr lkgc jv".
strip.gsub("\n", '').gsub(" ", '').squeeze('').chars.to_a
output = "our language is impossible to understand there are
twenty six factorial possibilities so it is okay if you want to just give up".
strip.gsub("\n", '').gsub(" ", '').squeeze('').chars.to_a
map = Hash[input.zip(output)]
# the problem gives you these
map['y'] = 'a'
map['e'] = 'o'
map['z'] = 'q'
map[' '] = ' '
# solve for missing k/v
missing_key = ""
('a'..'z').to_a.each do |c|
if !map.has_key? c
missing_key = c
end
end
missing_value = ""
('a'..'z').to_a.each do |c|
if !map.has_value? c
missing_value = c
end
end
map[missing_key] = missing_value
string = []
line_number = 0;
File.open(ARGV[0], 'r') do |f|
f.each_line do |line|
if line_number == 0 then
line_number = 1;
next
end
string.clear
string << "Case ##{line_number}: "
line_number = line_number + 1
line.each_char do |c|
string << map[c]
end
puts string.join
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment