Skip to content

Instantly share code, notes, and snippets.

@christianroman
Last active Aug 29, 2015
Embed
What would you like to do?
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