Last active
August 29, 2015 14:13
-
-
Save christianroman/c2ecd05cb5bd98d365e9 to your computer and use it in GitHub Desktop.
Facebook Hacker Cup 2015 Qualification Round: Cooking the Books
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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