Created
December 2, 2020 07:27
-
-
Save felix-larsen/d6d97c3da8ff9d239b4aafdd346074b2 to your computer and use it in GitHub Desktop.
2nd December solution - Advent of Code 2020
This file contains 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
let validPasswordsCount = passwords | |
.map { password -> DataEntry in | |
let substrings = password.split(separator: " ") | |
let minMaxSub = substrings[0].split(separator: "-") | |
return DataEntry(min: Int(minMaxSub[0])!, max: Int(minMaxSub[1])!, char: substrings[1].first!, password: String(substrings[2])) | |
} | |
.filter(isValid_1) | |
.count | |
print(validPasswordsCount) | |
func isValid_1(entry: DataEntry) -> Bool { | |
let occurrences = entry.password.countOccurrencesOf(char: entry.char) | |
return occurrences >= entry.min && occurrences <= entry.max | |
} |
This file contains 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
let validPasswordsCount = passwords | |
.map { password -> DataEntry in | |
let substrings = password.split(separator: " ") | |
let minMaxSub = substrings[0].split(separator: "-") | |
return DataEntry(min: Int(minMaxSub[0])!, max: Int(minMaxSub[1])!, char: substrings[1].first!, password: String(substrings[2])) | |
} | |
.filter(isValid_2) | |
.count | |
print(validPasswordsCount) | |
func isValid_2(entry: DataEntry) -> Bool { | |
let pos1valid = entry.password.charAt(entry.min-1) == entry.char | |
let pos2valid = entry.password.charAt(entry.max-1) == entry.char | |
return pos1valid != pos2valid | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment