Skip to content

Instantly share code, notes, and snippets.

@groob
Created December 4, 2019 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groob/839944a64e03541fd323f3fcf0278995 to your computer and use it in GitHub Desktop.
Save groob/839944a64e03541fd323f3fcf0278995 to your computer and use it in GitHub Desktop.
extension Int {
func digits() -> [Int] {
var newN = self
var result: [Int] = []
while newN > 0 {
let r = newN % 10
newN = newN / 10
result.append(r)
}
result.reverse()
return result
}
}
func isValid(_ digits: [Int]) -> Bool {
return digits == digits.sorted() && digits.contains { d in digits.filter { $0 == d }.count == 2 }
}
let result =
(136760...595730)
.map { $0.digits() }
.filter { isValid($0) }
.count
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment