Skip to content

Instantly share code, notes, and snippets.

@christianroman
Last active August 29, 2015 14:13
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 christianroman/c2ecd05cb5bd98d365e9 to your computer and use it in GitHub Desktop.
Save christianroman/c2ecd05cb5bd98d365e9 to your computer and use it in GitHub Desktop.
Facebook Hacker Cup 2015 Qualification Round: Cooking the Books
import Foundation
let location = "~/input.in".stringByExpandingTildeInPath
let content = String(contentsOfFile: location, encoding: NSUTF8StringEncoding, error: nil)!
var lines = split(content, { $0 == "\n"}, maxSplit: Int.max, allowEmptySlices: false)
let cases = lines.removeAtIndex(0)
for (i, n) in enumerate(lines) {
let digits = map(n) { String($0).toInt()! }
var (minSwap, maxSwap) = (digits, digits)
let numMin = digits.filter({ $0 != 0 }).reduce(Int.max, { min($0, $1) })
let numMax = digits.reduce(Int.min, { max($0, $1) })
if let index = find(digits, numMin) {
swap(&minSwap[index], &minSwap[0])
}
if let index = find(digits, numMax) {
swap(&maxSwap[index], &maxSwap[0])
}
let result = "".join(minSwap.map({ "\($0)" })) + " " + "".join(maxSwap.map({ "\($0)" }))
println("Case #\(i + 1): \(result)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment