diff options
| author | NSDepression <avimanyu.apple@gmail.com> | 2025-12-03 11:56:18 +0530 |
|---|---|---|
| committer | NSDepression <avimanyu.apple@gmail.com> | 2025-12-03 11:56:18 +0530 |
| commit | 881957f4f0865000ab978222c9cb694c73180995 (patch) | |
| tree | 9fb3fc40551ff74d21dceabc37afefb957f238eb /Day 2/Day_02.swift | |
| parent | 3a93d2d546476c3fb24f72b50e830930a321f969 (diff) | |
| download | AoC-2025-881957f4f0865000ab978222c9cb694c73180995.tar.gz AoC-2025-881957f4f0865000ab978222c9cb694c73180995.zip | |
Day 2.2
Diffstat (limited to 'Day 2/Day_02.swift')
| -rw-r--r-- | Day 2/Day_02.swift | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/Day 2/Day_02.swift b/Day 2/Day_02.swift index 6d682bd..dc991d6 100644 --- a/Day 2/Day_02.swift +++ b/Day 2/Day_02.swift @@ -37,7 +37,7 @@ print("=======Part 1=======\n\(invalidIdSum)") invalidIdSum = 0 func isRepeating(_ x: Int) -> Bool { - if x == 0 { return false } + if x < 10 { return false } var digits = 0 var temp = x while temp > 0 { @@ -48,19 +48,16 @@ func isRepeating(_ x: Int) -> Bool { for period in 1...(digits / 2) { guard digits % period == 0 else { continue } var divisor = 1 - for _ in 0..<period { - divisor *= 10 - } + for _ in 0..<period { divisor *= 10 } let pattern = x % divisor + guard pattern != 0 else { continue } var reconstructed = 0 var multiplier = 1 for _ in 0..<(digits / period) { reconstructed += pattern * multiplier multiplier *= divisor } - if reconstructed == x { - return true - } + if reconstructed == x { return true } } return false } @@ -72,4 +69,22 @@ ranges.forEach { range in } } } -print("=======Part 2=======\n\(invalidIdSum)")
\ No newline at end of file +print("=======Part 2=======\n\(invalidIdSum)") + +invalidIdSum = 0 +ranges.forEach { group in + for id in group { + let numString = String(id) + let numstrLength = numString.count + if numstrLength < 2 { continue } + for patternLength in 1...(numstrLength / 2) { + let remainder = numString.replacingOccurrences(of: numString[..<numString.index(numString.startIndex, offsetBy: patternLength)], with: "") + if remainder.isEmpty { + invalidIdSum += id + break + } + } + } +} + +print("=======Part 2.2=======\n\(invalidIdSum)")
\ No newline at end of file |
