-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.py
137 lines (125 loc) · 3.81 KB
/
day4.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import re
fp = open("d4input.txt", "r")
# read through each line until we get to a blank line which is a new passport
# go through each line and check for required fields
# update counter.
def checkbyr(l):
# byr (Birth Year) - four digits; at least 1920 and at most 2002.
if "byr" in l:
x = l.split("byr:")
y = x[1].split(" ")
z = y[0].strip() # just in case there are some extra spaces in there
if len(z) == 4 and 1920 <= int(z) <= 2002:
return True
return False
def checkiyr(l):
# iyr (Issue Year) - four digits; at least 2010 and at most 2020.
if "iyr" in l:
x = l.split("iyr:")
y = x[1].split(" ")
z = y[0].strip() # just in case there are some extra spaces in there
if len(z) == 4 and 2010 <= int(z) <= 2020:
return True
return False
def checkeyr(l):
# eyr (Expiration Year) - four digits; at least 2020 and at most 2030.
if "eyr" in l:
x = l.split("eyr:")
y = x[1].split(" ")
z = y[0].strip() # just in case there are some extra spaces in there
if len(z) == 4 and 2020 <= int(z) <= 2030:
return True
return False
def checkhgt(l):
# hgt (Height) - a number followed by either cm or in:
if "hgt" in l:
x = l.split("hgt:")
y = x[1].split(" ")
zz = y[0].strip() # just in case there are some extra spaces in there
if "cm" in y[0]:
# If cm, the number must be at least 150 and at most 193.
z = zz.split("cm")
if 150 <= int(z[0]) <= 193:
return True
if "in" in y[0]:
# If in, the number must be at least 59 and at most 76.
z = zz.split("in")
if 59 <= int(z[0]) <= 76:
return True
return False
def checkhcl(l): # might need to check this
# hcl (Hair Color) - a # followed by exactly six characters 0-9 or a-f.
if "hcl" in l:
x = l.split("hcl:")
y = x[1].split(" ")
yy = y[0].strip()
z = re.sub('[^a-f0-9#]', '', yy)
if len(z) == 7 and z[0] == "#":
return True
return False
def checkecl(l):
# ecl (Eye Color) - exactly one of: amb blu brn gry grn hzl oth
el = ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]
if "ecl" in l:
x = l.split("ecl:")
y = x[1].split(" ")
z = y[0].strip()
if z in el:
return True
return False
def checkpid(l):
# pid (Passport ID) - a nine-digit number, including leading zeroes.
if "pid" in l:
x = l.split("pid:")
y = x[1].split(" ")
z = y[0].strip()
if len(z) == 9:
return True
return False
valid = 0
passport = []
# read through each line add it to the passport list until we get to blank line then check then pause to check the passport
def checkPassport():
global valid
global passport
byr = False
iyr = False
eyr = False
hgt = False
hcl = False
ecl = False
pid = False
for l in passport:
if not byr:
byr = checkbyr(l)
if not iyr:
iyr = checkiyr(l)
if not eyr:
eyr = checkeyr(l)
if not hgt:
hgt = checkhgt(l)
if not hcl:
hcl = checkhcl(l)
if not ecl:
ecl = checkecl(l)
if not pid:
pid = checkpid(l)
# print(byr,iyr,eyr, hgt, hcl, ecl, pid)
if byr and iyr and eyr and hgt and hcl and ecl and pid:
valid += 1
passport = []
def readLines(text):
global passport
while True:
buffer = text.readline()
if buffer != "\n":
passport.append(buffer)
if buffer == "\n":
checkPassport()
if buffer == "":
checkPassport()
break
# checkPassport()
readLines(fp)
print(valid)
print(passport)