Skip to content

Instantly share code, notes, and snippets.

@felix-larsen
Created December 4, 2020 06:12
Show Gist options
  • Save felix-larsen/f146deb3a908bb064d8ab29f3dd5163f to your computer and use it in GitHub Desktop.
Save felix-larsen/f146deb3a908bb064d8ab29f3dd5163f to your computer and use it in GitHub Desktop.
4th December - Advent of Code 2020 - swift
var validPassportsCount = 0
let keys: [String] = [
"ecl",
"pid",
"eyr",
"hcl",
"byr",
"iyr",
"hgt"
]
for p in passports {
var validKeyCount = 0
if p.count >= 7 {
for line in p {
let keyValue = line.components(separatedBy: [":"])
let key = keyValue[0]
if keys.contains(key) {
validKeyCount += 1
}
}
}
if validKeyCount == 7 {
validPassportsCount += 1
}
}
print(validPassportsCount)
let eclValues = [
"amb", "blu", "brn", "gry", "grn", "hzl", "oth"]
let colorValues = [
"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]
var validPassportsCount = 0
let keys: [String: (String) -> Bool] = [
"ecl": { value in
eclValues.contains(value)
},
"pid": { value in
value.count == 9 && Int(value) != nil
},
"eyr": { value in
value.count == 4 && Int(value) != nil && Int(value)! >= 2020 && Int(value)! <= 2030
},
"hcl": { value in
let a = Array(value)
if a[0] != "#" && a.count != 7 {
return false
}
for i in 1...6 {
if !colorValues.contains(String(a[i])) {
return false
}
}
return true
},
"byr": { value in
value.count == 4 && Int(value) != nil && Int(value)! >= 1920 && Int(value)! <= 2002
},
"iyr": { value in
value.count == 4 && Int(value) != nil && Int(value)! >= 2010 && Int(value)! <= 2020
},
"hgt": { value in
let letters = Array(String(value))
if letters.count == 4 {
let height = Int(String(letters[0]) + String(letters[1]))
if height == nil {
return false
}
if height! <= 76 && height! >= 59 {
if (String(letters[2]) + String(letters[3])) == "in" {
return true
}
}
}
if letters.count == 5 {
let height = Int(String(letters[0]) + String(letters[1]) + String(letters[2]))
if height == nil {
return false
}
if height! <= 193 && height! >= 150 {
if (String(letters[3]) + String(letters[4])) == "cm" {
return true
}
}
}
return false
}
]
for p in passports {
var validKeyCount = 0
if p.count >= 7 {
for line in p {
let keyValue = line.components(separatedBy: [":"])
let valid = keys.first { (key,validationFunc) -> Bool in
key == keyValue[0] && validationFunc(keyValue[1])
}
if valid != nil {
validKeyCount += 1
} else {
print("invalid\(keyValue)")
}
}
}
if validKeyCount == 7 {
validPassportsCount += 1
}
}
print(validPassportsCount)
let filename = "/Users/felix/xCodeProjects/AdventOfCode2020.playground/Resources/december04.txt"
let contents = try! String(contentsOfFile: filename)
let lines = contents.components(separatedBy: CharacterSet.whitespacesAndNewlines)
var passports = [[String]]()
var passport = [String]()
for line in lines {
if line == "" {
passports.append(passport)
passport = [String]()
} else {
passport.append(String(line))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment