-
Notifications
You must be signed in to change notification settings - Fork 0
/
jumpingOnClouds.swift
48 lines (36 loc) · 1.3 KB
/
jumpingOnClouds.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import Foundation
/*
* Complete the 'jumpingOnClouds' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY c as parameter.
*/
func jumpingOnClouds(c: [Int]) -> Int {
var jump = 0
var location = 0
while location < c.count - 1 {
if (location + 2 < c.count) &&
(c[location + 2] == 0) {
location += 2
} else {
location += 1
}
jump += 1
}
return jump
}
let stdout = ProcessInfo.processInfo.environment["OUTPUT_PATH"]!
FileManager.default.createFile(atPath: stdout, contents: nil, attributes: nil)
let fileHandle = FileHandle(forWritingAtPath: stdout)!
guard let n = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
guard let cTemp = readLine()?.replacingOccurrences(of: "\\s+$", with: "", options: .regularExpression) else { fatalError("Bad input") }
let c: [Int] = cTemp.split(separator: " ").map {
if let cItem = Int($0) {
return cItem
} else { fatalError("Bad input") }
}
guard c.count == n else { fatalError("Bad input") }
let result = jumpingOnClouds(c: c)
fileHandle.write(String(result).data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)