diff options
| author | NSDepression <avimanyu.apple@gmail.com> | 2025-12-03 11:00:19 +0530 |
|---|---|---|
| committer | NSDepression <avimanyu.apple@gmail.com> | 2025-12-03 11:00:19 +0530 |
| commit | 3a93d2d546476c3fb24f72b50e830930a321f969 (patch) | |
| tree | 0d5c0e6029fd88155d6b49d4c6d32c9c58beeeaa /Day 2 | |
| parent | f35667631aca011ee5f99772e99f9264e06f7ca1 (diff) | |
| download | AoC-2025-3a93d2d546476c3fb24f72b50e830930a321f969.tar.gz AoC-2025-3a93d2d546476c3fb24f72b50e830930a321f969.zip | |
Day 2
Diffstat (limited to 'Day 2')
| -rw-r--r-- | Day 2/Day_02.swift | 75 | ||||
| -rw-r--r-- | Day 2/input.txt | 1 |
2 files changed, 76 insertions, 0 deletions
diff --git a/Day 2/Day_02.swift b/Day 2/Day_02.swift new file mode 100644 index 0000000..6d682bd --- /dev/null +++ b/Day 2/Day_02.swift @@ -0,0 +1,75 @@ +#!/usr/bin/swift +import Foundation + +guard CommandLine.arguments.count >= 2 else { + fatalError("Usage: ./Day_01 <input.txt>") +} + + +let ranges: [ClosedRange<Int>] = try! String.init(contentsOfFile: CommandLine.arguments[1], + encoding: .utf8) +.split(separator: ",") +.map { rangeString in + guard let separatorIdx = rangeString.firstIndex(of: "-"), + let lower = Int(rangeString[..<separatorIdx]), + let upper = Int(rangeString[rangeString.index(after: separatorIdx)...]) else { + fatalError("L16 : [ERROR] Failed to create ClosedRange!") + } + return lower...upper +} + +var invalidIdSum: Int = 0 + +for range in ranges { + for number in range { + let numString = String(number) + let length = numString.count + guard length.isMultiple(of: 2) else { continue } + let mid = numString.index(numString.startIndex, offsetBy: length / 2) + let left = numString[numString.startIndex..<mid] + let right = numString[mid..<numString.endIndex] + if left == right { + invalidIdSum += number + } + } +} +print("=======Part 1=======\n\(invalidIdSum)") + +invalidIdSum = 0 +func isRepeating(_ x: Int) -> Bool { + if x == 0 { return false } + var digits = 0 + var temp = x + while temp > 0 { + digits += 1 + temp /= 10 + } + if digits < 2 { return false } + for period in 1...(digits / 2) { + guard digits % period == 0 else { continue } + var divisor = 1 + for _ in 0..<period { + divisor *= 10 + } + let pattern = x % divisor + var reconstructed = 0 + var multiplier = 1 + for _ in 0..<(digits / period) { + reconstructed += pattern * multiplier + multiplier *= divisor + } + if reconstructed == x { + return true + } + } + return false +} + +ranges.forEach { range in + for id in range { + if isRepeating(id) { + invalidIdSum += id + } + } +} +print("=======Part 2=======\n\(invalidIdSum)")
\ No newline at end of file diff --git a/Day 2/input.txt b/Day 2/input.txt new file mode 100644 index 0000000..bfcd4e3 --- /dev/null +++ b/Day 2/input.txt @@ -0,0 +1 @@ +5959566378-5959623425,946263-1041590,7777713106-7777870316,35289387-35394603,400-605,9398763-9592164,74280544-74442206,85684682-85865536,90493-179243,202820-342465,872920-935940,76905692-76973065,822774704-822842541,642605-677786,3759067960-3759239836,1284-3164,755464-833196,52-128,3-14,30481-55388,844722790-844967944,83826709-83860070,9595933151-9595993435,4216-9667,529939-579900,1077949-1151438,394508-486310,794-1154,10159-17642,5471119-5683923,16-36,17797-29079,187-382
\ No newline at end of file |
