Skip to content

Instantly share code, notes, and snippets.

@azurast
Last active August 14, 2021 02:02
Show Gist options
  • Save azurast/5fd3a413e5d23444578c85d423506da4 to your computer and use it in GitHub Desktop.
Save azurast/5fd3a413e5d23444578c85d423506da4 to your computer and use it in GitHub Desktop.
Jumping on Clouds - Swift
var c: [Int] = [0, 0, 1, 0, 0, 1, 0]
print("array \(c)")
var numberOfJumps = 0
var i = 0
while i < c.count {
if (i < c.count-1) {
print("i \(i)")
if (i < c.count-2) {
if (c[i+2] == 0) {
// jump twice
i += 2
} else if (c[i+1] == 0) {
// jump once
i += 1
}
} else {
if (c[i+1] == 0) {
// jump once
i += 1
}
}
numberOfJumps += 1
} else {
break
}
}
print("numberOfJumps \(numberOfJumps)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment