-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.py
39 lines (30 loc) · 1.29 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
must_keys = {"byr","iyr", "eyr","hgt", "hcl","ecl","pid"}
eye_see = {"amb", "blu", "brn", "gry", "grn", "hzl", "oth"}
def reader(filename):
with open(filename) as f:
content = f.read()
out1=[x.split() for x in content.split("\n\n")]
return [dict([y.split(":") for y in x]) for x in out1]
def checker(entries):
return sum([must_keys.issubset(set(x.keys())) for x in entries])
def legit_checker(entries):
y= []
for x in entries:
if must_keys.issubset(set(x.keys())):
if (int(x["byr"]) >= 1920 and int(x["byr"]) <= 2002) and \
(int(x["iyr"]) >= 2010 and int(x["iyr"]) <= 2020) and \
(int(x["eyr"]) >= 2020 and int(x["eyr"]) <= 2030) and \
len(x["hcl"]) == 7 and x["hcl"][0] == "#" and \
len(x["pid"]) == 9 and \
(x["ecl"] in eye_see):
if x["hgt"][-2:] == "cm":
if int(x["hgt"][:-2]) >=150 and int(x["hgt"][:-2]) <=193:
y.append(True)
elif x["hgt"][-2:] == "in":
if int(x["hgt"][:-2]) >=59 and int(x["hgt"][:-2]) <=76:
y.append(True)
return sum(y)
if __name__ == "__main__":
entries = reader("4.txt")
print(checker(entries))
print(legit_checker(entries))