Last active
December 3, 2024 01:25
-
-
Save kevin49999/5891cfe76762d2ffbd20aa63fb53db13 to your computer and use it in GitHub Desktop.
aoc-d2-pt2-2024
This file contains hidden or 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
| func levelSafe(ints: [Int]) -> Bool { | |
| let increasing: Bool | |
| let diff = ints[1] - ints[0] | |
| if diff > 0 && diff <= 3 { | |
| increasing = true | |
| } else if diff < 0 && diff >= -3 { | |
| increasing = false | |
| } else { | |
| return false | |
| } | |
| for i in 1..<ints.count - 1 { | |
| let diff = ints[i + 1] - ints[i] | |
| if increasing && (diff <= 0 || diff > 3) { return false } | |
| if !increasing && (diff >= 0 || diff < -3) { return false } | |
| } | |
| return true | |
| } | |
| func solution(input: String) -> Int { | |
| var result = 0 | |
| let lines = input.split(separator: "\n") | |
| var failedLevels = [[Int]]() | |
| for line in lines { | |
| let ints: [Int] = line.split(separator: " ").map { Int($0)! } | |
| if levelSafe(ints: ints) { | |
| result += 1 | |
| } else { | |
| failedLevels.append(ints) | |
| } | |
| } | |
| for failed in failedLevels { | |
| var mutations = [[Int]]() | |
| for i in 0..<failed.count { | |
| var cp = failed | |
| cp.remove(at: i) | |
| mutations.append(cp) | |
| } | |
| for mutation in mutations { | |
| if levelSafe(ints: mutation) { | |
| result += 1 | |
| break | |
| } | |
| } | |
| } | |
| return result | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment