Swift Solution here. By reading the statement I felt easy but took almost same time as yesterday to solve this as well.
import Cocoa let start: Int = 134792 let end: Int = 675810 extension BinaryInteger { var digits: [Int] { return String(describing: self).compactMap { Int(String($0)) } } } func partOne() { var count = 0 for number in (start ... end) { let array = number.digits if array.sorted() == array { for pair in zip(array, array.dropFirst()) { if pair.0 == pair.1 { count = count + 1 break; } } } } print(count) } func partTwo() { var total = 0 for number in (start ... end) { let array = number.digits if array.sorted() == array { if (hasAtLeast2Pair(array)) { total += 1 } } } print(total) } func hasAtLeast2Pair(_ array: Array<Int>) -> Bool { var count = 0 var dict: [Int:Int] = [:] var dupe = false for pair in zip(array, array.dropFirst()) { if pair.0 == pair.1 { count += 1 dict[pair.0] = count } else { if count == 1 { dupe = true break } count = 0 } } if count == 1 { dupe = true } return dupe } partOne() partTwo()
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Swift Solution here. By reading the statement I felt easy but took almost same time as yesterday to solve this as well.