Skip to content

Instantly share code, notes, and snippets.

@omaarr90
Created February 8, 2017 18:59
Show Gist options
  • Save omaarr90/19486b8cb5aa2462b17e5def9b14b319 to your computer and use it in GitHub Desktop.
Save omaarr90/19486b8cb5aa2462b17e5def9b14b319 to your computer and use it in GitHub Desktop.
Elves and Their Codes
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
let alphabitDict = [0: "A", 1: "B", 2: "C", 3: "D", 4: "E", 5: "F", 6: "G", 7: "H", 8: "I", 9: "J", 10: "K", 11: "L", 12: "M", 13: "N", 14: "O", 15: "P", 16: "Q", 17: "R", 18: "S", 19: "T",20: "U", 21: "V", 22: "W", 23: "X", 24: "Y", 25: "Z", 26: " "]
var initalRowOrder = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
var initalColOrder = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
func decodeMessage(_ message: [Int]) -> String
{
var gift = ""
for i in 0..<message.count {
// let j = i - 1
var index = message[i]
index = index - 1
let number = initalRowOrder[index]
let letter = initalColOrder[number]
initalColOrder = swapWithBeforeLast(initalColOrder, index: number)
initalRowOrder = swapWithBeforeLast(initalRowOrder, index: index)
gift.append(alphabitDict[letter]!)
}
return gift
}
func swapWithBeforeLast( _ arr: [Int], index: Int) -> [Int]
{
var arr = arr
if index == 26 {
return arr
}
let number = arr.remove(at: index)
arr.insert(number, at: 25)
return arr
}
let Message2 = decodeMessage([13, 1, 10, 3, 12, 18, 27, 2, 25, 27, 22, 7, 5, 23, 24])
let Message3 = decodeMessage([18, 5, 10, 11, 5, 5, 18])
//
//let Message4 = decodeMessage([13, 1, 10, 3, 12, 18, 27, 2, 25, 27, 22, 7, 5, 23, 24])
//
//
//let Message5 = decodeMessage([13, 1, 10, 3, 12, 18, 27, 2, 25, 27, 22, 7, 5, 23, 24])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment