This repository was archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path04_password_processing.py
56 lines (41 loc) · 1.74 KB
/
04_password_processing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
######################################
# --- Day 4: Passport Processing --- #
######################################
import AOCUtils
decChars = set("0123456789")
hexChars = set("0123456789abcdef")
checks1 = [
lambda pp: all(field in pp for field in ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"])
]
checks2 = [
lambda pp: set(pp["byr"]) <= decChars and 1920 <= int(pp["byr"]) <= 2002,
lambda pp: set(pp["iyr"]) <= decChars and 2010 <= int(pp["iyr"]) <= 2020,
lambda pp: set(pp["eyr"]) <= decChars and 2020 <= int(pp["eyr"]) <= 2030,
lambda pp: set(pp["hgt"][:-2]) <= decChars and \
((pp["hgt"][-2:] == "cm" and 150 <= int(pp["hgt"][:-2]) <= 193) or \
(pp["hgt"][-2:] == "in" and 59 <= int(pp["hgt"][:-2]) <= 76)),
lambda pp: len(pp["hcl"]) == 7 and pp["hcl"][0] == "#" and set(pp["hcl"][1:]) <= hexChars,
lambda pp: pp["ecl"] in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"],
lambda pp: len(pp["pid"]) == 9 and set(pp["pid"]) <= decChars
]
def isValid1(passport):
return all(check(passport) for check in checks1)
def isValid2(passport):
return isValid1(passport) and all(check(passport) for check in checks2)
######################################
rawInput = AOCUtils.loadInput(4)
for i in range(len(rawInput)):
if rawInput[i] == "": rawInput[i] = "\n"
rawPassports = " ".join(rawInput).split(" \n ")
passports = []
for rawPassport in rawPassports:
passport = dict()
for kvp in rawPassport.split():
k, v = kvp.split(":")
passport[k] = v
passports.append(passport)
p1 = sum(isValid1(passport) for passport in passports)
print("Part 1: {}".format(p1))
p2 = sum(isValid2(passport) for passport in passports)
print("Part 2: {}".format(p2))
AOCUtils.printTimeTaken()