-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04.py
executable file
·119 lines (89 loc) · 3.34 KB
/
04.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python3
import re # re.split
# Eingabe lesen
today = __file__.rsplit("/", 1)[-1].removesuffix(".py")
with open(f"input/{today}.txt") as infile:
input = infile.read().split("\n\n")
class PassportValidator:
def __init__(self):
self.requiredFields = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid']
self.optionalFields = ['cid']
# Passport einlesen
def parse_passport(self, passport):
passportDataRaw = re.split(" |\n", passport)
passportData = {}
for dataset in passportDataRaw:
var, value = dataset.split(":")
passportData[var] = value
return passportData
# Alle notwendigen Felder enthalten
def passport_contains_required_fields(self, passport):
passportData = self.parse_passport(passport)
for requiredField in self.requiredFields:
if requiredField not in passportData:
return False
return True
# Alle notwendigen Felder enthalten und Daten sind gültig
def passport_is_valid(self, passport):
if not self.passport_contains_required_fields(passport):
return False
passportData = self.parse_passport(passport)
for key, value in passportData.items():
if not self.field_is_valid(key, value):
return False
return True
# Prüfe angegebenes Feld auf Gültigkeit
def field_is_valid(self, field, value):
return {
"byr": self.validate_byr,
"iyr": self.validate_iyr,
"eyr": self.validate_eyr,
"hgt": self.validate_hgt,
"hcl": self.validate_hcl,
"ecl": self.validate_ecl,
"pid": self.validate_pid,
"cid": self.validate_cid
}.get(field, lambda: "Ungültiges Feld!")(value)
def validate_byr(self, value):
try:
return len(value) == 4 and 1920 <= int(value) <= 2002
except:
return False
def validate_iyr(self, value):
try:
return len(value) == 4 and 2010 <= int(value) <= 2020
except:
return False
def validate_eyr(self, value):
try:
return len(value) == 4 and 2020 <= int(value) <= 2030
except:
return False
def validate_hgt(self, value):
if value[-2:] == "cm":
return 150 <= int(value[:-2]) <= 193
elif value[-2:] == "in":
return 59 <= int(value[:-2]) <= 76
else:
return False
def validate_hcl(self, value):
return re.match("#[a-h0-9]{6}$", value)
def validate_ecl(self, value):
return value in "amb blu brn gry grn hzl oth".split(" ")
def validate_pid(self, value):
return re.match("\d{9}$", value)
def validate_cid(self, value):
return True
# Teil 1
validator = PassportValidator()
numValidPassports = 0
for passport in input:
if validator.passport_contains_required_fields(passport):
numValidPassports += 1
print(f"Anzahl Reisepässe mit allen Daten exkl. cid: {numValidPassports}")
# Teil 2
numValidPassports = 0
for passport in input:
if validator.passport_is_valid(passport):
numValidPassports += 1
print(f"Anzahl Reisepässe mit gültigen Daten exkl. cid: {numValidPassports}")