summaryrefslogtreecommitdiffstats
path: root/Day 2/Day_02.swift
diff options
context:
space:
mode:
Diffstat (limited to 'Day 2/Day_02.swift')
-rw-r--r--Day 2/Day_02.swift31
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