Skip to content

Commit 623f5ba

Browse files
committed
Day 4
1 parent 7948d29 commit 623f5ba

File tree

2 files changed

+1291
-0
lines changed

2 files changed

+1291
-0
lines changed

4.py

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import os
2+
3+
4+
#initial
5+
def validatePassport(pp):
6+
if len(pp) < 7:
7+
return False
8+
fields = pp.keys()
9+
if "byr" not in fields:
10+
return False
11+
if "iyr" not in fields:
12+
return False
13+
if "eyr" not in fields:
14+
return False
15+
if "hgt" not in fields:
16+
return False
17+
if "hcl" not in fields:
18+
return False
19+
if "ecl" not in fields:
20+
return False
21+
if "pid" not in fields:
22+
return False
23+
return True
24+
25+
def makePassport(text):
26+
pp = {}
27+
for line in text:
28+
line = line[:-1]
29+
pairs = line.split(" ")
30+
for pair in pairs:
31+
(key, val) = pair.split(":")
32+
pp[key] = val
33+
34+
return pp
35+
36+
with open("4.txt") as file:
37+
total = 0
38+
valid = 0
39+
invalid = 0
40+
41+
lines = file.readlines()
42+
text = []
43+
for line in lines:
44+
if len(line) < 2:
45+
total = total + 1
46+
pp = makePassport(text)
47+
if validatePassport(pp):
48+
valid = valid + 1
49+
else:
50+
invalid = invalid + 1
51+
text = []
52+
else:
53+
text.append(line)
54+
total = total + 1
55+
pp = makePassport(text)
56+
if validatePassport(pp):
57+
valid = valid + 1
58+
else:
59+
invalid = invalid + 1
60+
61+
print(valid, invalid, total)
62+
63+
#part2
64+
def validatePassport2(pp):
65+
#print("------")
66+
try:
67+
if len(pp) < 7:
68+
#print("too few")
69+
return False
70+
fields = pp.keys()
71+
if "byr" not in fields or int(pp["byr"]) < 1920 or int(pp["byr"]) > 2002:
72+
#print("byr")
73+
#print(pp["byr"])
74+
return False
75+
if "iyr" not in fields or int(pp["iyr"]) < 2010 or int(pp["iyr"]) > 2020:
76+
#print("iyr")
77+
#print(pp["iyr"])
78+
return False
79+
if "eyr" not in fields or int(pp["eyr"]) < 2020 or int(pp["eyr"]) > 2030:
80+
#print("eyr")
81+
#print(pp["eyr"])
82+
return False
83+
if "hgt" not in fields:
84+
#print("hgt")
85+
return False
86+
if pp["hgt"][-2:] == "cm":
87+
#print("cm")
88+
if int(pp["hgt"][:-2]) < 150 or int(pp["hgt"][:-2]) > 193:
89+
#print("cm-bad")
90+
#print(pp["hgt"])
91+
return False
92+
else:
93+
if pp["hgt"][-2:] == "in":
94+
#print("in")
95+
if int(pp["hgt"][:-2]) < 59 or int(pp["hgt"][:-2]) > 76:
96+
#print("in-bad")
97+
#print(pp["hgt"])
98+
return False
99+
else:
100+
#print("hgt unit")
101+
#print(pp["hgt"])
102+
return False
103+
if "hcl" not in fields or pp["hcl"][0] != "#" or len(pp["hcl"]) != 7 or int(pp["hcl"][1:], 16) < 0:
104+
#print("hcl")
105+
#print(pp["hcl"])
106+
return False
107+
if "ecl" not in fields or pp["ecl"] not in ["amb","blu","brn","gry","grn","hzl","oth"]:
108+
#print("ecl")
109+
#print(pp["ecl"])
110+
return False
111+
if "pid" not in fields or len(pp["pid"]) != 9 or int(pp["pid"]) < 0 or int(pp["pid"]) > 999999999:
112+
#print("pid")
113+
#print(pp["pid"])
114+
return False
115+
#print("*Valid")
116+
#print("========")
117+
#print(pp["pid"],len(pp["pid"]))
118+
return True
119+
except:
120+
return False
121+
122+
with open("4.txt") as file:
123+
total = 0
124+
valid = 0
125+
invalid = 0
126+
127+
lines = file.readlines()
128+
text = []
129+
for line in lines:
130+
if len(line) < 2:
131+
total = total + 1
132+
#print("------" + str(total))
133+
#print(text)
134+
pp = makePassport(text)
135+
result = validatePassport2(pp)
136+
if result:
137+
valid = valid + 1
138+
else:
139+
invalid = invalid + 1
140+
#try:
141+
# print(str(total),pp["pid"],str(result), sep=",")
142+
#except:
143+
# print(str(total), "missing", str(result), sep=",")
144+
145+
text = []
146+
else:
147+
text.append(line)
148+
total = total + 1
149+
pp = makePassport(text)
150+
if validatePassport2(pp):
151+
valid = valid + 1
152+
else:
153+
invalid = invalid + 1
154+
155+
print(valid, invalid, total)

0 commit comments

Comments
 (0)