Skip to content

Instantly share code, notes, and snippets.

@matteodanelli
Created March 25, 2017 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matteodanelli/352176b8fa5d55711e2dca605658df0f to your computer and use it in GitHub Desktop.
Save matteodanelli/352176b8fa5d55711e2dca605658df0f to your computer and use it in GitHub Desktop.
Google Code Jam stdin reader - Swift version
import Foundation
func readInput() {
let numberOfCases = Int(readLine() ?? "0")!
for _case in 0..<numberOfCases {
guard let currentLine = readLine() as String! else {
return
}
let lineValues = getIntegersOf(line: currentLine, separatedBy: " ")
let n = lineValues[0]
let m = lineValues[1]
print("Case #\(_case): \(n + m) \(n * m)");
}
}
func getIntegersOf(line: String, separatedBy separator: String) -> [Int] {
let stringArray = line.components(separatedBy: separator)
// WARNING: if separator is " " and there's a space after the last number then stringArray contains empty string!
return stringArray.map { Int($0)! }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment