Skip to content

Instantly share code, notes, and snippets.

@denpazakura
Last active December 19, 2023 21:46
Show Gist options
  • Save denpazakura/5b7895e31b59a1709e891903d9101609 to your computer and use it in GitHub Desktop.
Save denpazakura/5b7895e31b59a1709e891903d9101609 to your computer and use it in GitHub Desktop.
Tower breakers game algorithm
import Foundation
/*
* Complete the 'towerBreakers' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER n
* 2. INTEGER m
*/
func towerBreakers(n: Int, m: Int) -> Int {
return m == 1 || n % 2 == 0 ? 2 : 1 // solution
}
let stdout = ProcessInfo.processInfo.environment["OUTPUT_PATH"]!
FileManager.default.createFile(atPath: stdout, contents: nil, attributes: nil)
let fileHandle = FileHandle(forWritingAtPath: stdout)!
guard let t = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
for tItr in 1...t {
guard let firstMultipleInputTemp = readLine()?.replacingOccurrences(of: "\\s+$", with: "", options: .regularExpression) else { fatalError("Bad input") }
let firstMultipleInput = firstMultipleInputTemp.split(separator: " ").map{ String($0) }
guard let n = Int(firstMultipleInput[0])
else { fatalError("Bad input") }
guard let m = Int(firstMultipleInput[1])
else { fatalError("Bad input") }
let result = towerBreakers(n: n, m: m)
fileHandle.write(String(result).data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)
}
@denpazakura
Copy link
Author

The code above will only compile in the HackerRank online IDE, since there's no scope provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment