Created
December 4, 2019 18:44
-
-
Save groob/839944a64e03541fd323f3fcf0278995 to your computer and use it in GitHub Desktop.
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
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