Skip to content

Instantly share code, notes, and snippets.

@alobato
Created September 6, 2010 23:37
Show Gist options
  • Save alobato/567640 to your computer and use it in GitHub Desktop.
Save alobato/567640 to your computer and use it in GitHub Desktop.
def invert(code)
"#{code[1].chr}#{code[0].chr}"
end
def encrypt(zipcode)
codes = {
"00" => "AL",
"01" => "UG",
"10" => "CN",
"11" => "X8",
"20" => "EP",
"21" => "Z0",
"30" => "GR",
"31" => "1B",
"40" => "ID",
"41" => "3D",
"50" => "JS",
"51" => "4C",
"60" => "HQ",
"61" => "2A",
"70" => "FO",
"71" => "09",
"80" => "DM",
"81" => "Y7",
"90" => "BK",
"91" => "V5"
}
encrypted_zipcode = ''
index = 0
zipcode.each_char do |char| # 2
code_1 = codes[char + '1'] # Z0
inverted_code_1 = invert(code_1) # 0Z
code_0 = codes[char + '0'] # EP
inverted_code_0 = invert(code_0) # PE
if index.even?
partial = code_1
partial = inverted_code_1 if index > 3 && !encrypted_zipcode.include?(inverted_code_1)
else
partial = code_0
partial = inverted_code_0 if index > 3 && !encrypted_zipcode.include?(inverted_code_0)
end
encrypted_zipcode << partial
index += 1
end
encrypted_zipcode
end
def decrypt(encrypted_zipcode)
codes = {
"AL" => "0", "LA" => "0",
"UG" => "0", "GU" => "0",
"CN" => "1", "NC" => "1",
"X8" => "1", "8X" => "1",
"EP" => "2", "PE" => "2",
"Z0" => "2", "0Z" => "2",
"GR" => "3", "RG" => "3",
"1B" => "3", "B1" => "3",
"ID" => "4", "DI" => "4",
"3D" => "4", "D3" => "4",
"JS" => "5", "SJ" => "5",
"4C" => "5", "C4" => "5",
"HQ" => "6", "QH" => "6",
"2A" => "6", "A2" => "6",
"FO" => "7", "OF" => "7",
"09" => "7", "90" => "7",
"DM" => "8", "MD" => "8",
"Y7" => "8", "7Y" => "8",
"BK" => "9", "KB" => "9",
"V5" => "9", "5V" => "9"
}
zipcode = ''
(0...encrypted_zipcode.length).step(2) do |i|
code = "#{encrypted_zipcode[i].chr}#{encrypted_zipcode[i + 1].chr}"
zipcode << codes[code]
end
zipcode
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment