-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.py
79 lines (62 loc) · 1.75 KB
/
4.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
import re
ATTRS = ['ecl', 'pid', 'eyr', 'hcl', 'byr', 'iyr', 'hgt']
HCL_RE = re.compile(r'#[0-9a-f]{6}')
class Passport:
ecl = None
pid = None
eyr = None
hcl = None
byr = None
iyr = None
cid = None
hgt = None
@staticmethod
def parse(s):
p = Passport()
props = s.replace('\n', ' ').strip().split(' ')
for prop in props:
k, v = prop.split(':')
try:
if 'yr' in k:
v = int(v)
except:
pass
setattr(p, k, v)
return p
def is_byr_valid(self):
return 1920 <= self.byr <= 2002
def is_iyr_valid(self):
return 2010 <= self.iyr <= 2020
def is_eyr_valid(self):
return 2020 <= self.eyr <= 2030
def is_hgt_valid(self):
try:
if 'cm' in self.hgt:
return 150 <= int(self.hgt[:-2]) <= 193
if 'in' in self.hgt:
return 59 <= int(self.hgt[:-2]) <= 76
except:
pass
return False
def is_hcl_valid(self):
return HCL_RE.match(self.hcl)
def is_ecl_valid(self):
return self.ecl in ['amb','blu','brn','gry','grn','hzl','oth']
def is_pid_valid(self):
return len(self.pid) == 9 and int(self.pid)
def is_valid(self):
for a in ATTRS:
if not getattr(self, a):
return False
if not getattr(self, f'is_{a}_valid')():
return False
return True
with open("input4.txt") as f:
fi = f.read()
passports = fi.split('\n\n')
valid_passports = 0
for p in passports:
passport = Passport.parse(p)
if passport.is_valid():
valid_passports += 1
print(valid_passports)