Once of the frustrating things about getting started with Google Coding Competition questions was the lack of examples on how to read the file input in Swift.

Each question requires you to read input from the command line, and then print the answer in a specific format.

I created this cheat sheet to help me read this input in so I could get on with the question. It really comes down to this.

import Foundation

func readInput() -> Int {
    return readLine().flatMap { Int($0) }!
}

func readInput() -> [Int] {
    return readLine().flatMap { $0.split(separator: " ").compactMap { Int($0)} }!
}

func readInput() -> (Int, Int) {
    let inputs = readInput() as [Int]
    return (inputs[0], inputs[1])
}

func readInput() -> Double {
    return readLine().flatMap { Double($0) }!
}

func readInput() -> [Double] {
    return readLine().flatMap { $0.split(separator: " ").compactMap { Double($0)} }!
}

func readInput() -> (Double, Double) {
    let inputs = readInput() as [Double]
    return (inputs[0], inputs[1])
}

func readInput() -> String {
    return readLine()!
}

func readInput() -> [String] {
    let str = readLine()!
    return str.components(separatedBy: " ")
}

var t: Int = readInput()

for i in 1..<t + 1 {
    let nm: (Int, Int) = readInput()
    print("Case #\(i): \(nm.0) \(nm.1)")
}

Here is what my solution looks like to the Retype question of Google 2020.

import Foundation

func readInput() -> Int {
    return readLine().flatMap { Int($0) }!
}

func readInput() -> [Int] {
    return readLine().flatMap { $0.split(separator: " ").compactMap { Int($0)} }!
}

func readInput() -> (Int, Int, Int) {
    let inputs = readInput() as [Int]
    return (inputs[0], inputs[1], inputs[2])
}

let restartTime = 1

func worstCase(_ N: Int, _ K: Int, _ S: Int) -> Int {
    let firstLevels = K - 1
    return firstLevels + N + restartTime
}

func possibleCase(_ N: Int, _ K: Int, _ S: Int) -> Int {
    let firstLevels = K - 1 // 4
    let backward = K - S // 3
    let forward = N - S
    
    return firstLevels + backward + forward + restartTime
}

func bestCase(_ N: Int, _ K: Int, _ S: Int) -> Int {
    return min(worstCase(N, K, S), possibleCase(N, K, S))
}

var t: Int = readInput()

for i in 1..<t + 1 {
    let nks: (Int, Int, Int) = readInput()
    print("Case #\(i): \(bestCase(nks.0, nks.1, nks.2))")
}

Hope this helps. Good luck!