-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.py
118 lines (100 loc) · 3.06 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
from sys import argv
import sys
import re
def main():
if len(argv) != 2:
print("Usage: python program input.txt")
sys.exit(1)
elif ".txt" not in argv[1]:
print("Input must be a txt file")
sys.exit(1)
with open(argv[1]) as f:
input = f.read()
passports = readPassports(input)
validpassports = 0
for passport in passports:
if checkValidity(passport):
validpassports += 1
print(validpassports)
def readPassports(file):
inputfile = list()
passports = list()
for s in file.split("\n\n"):
inputfile.append(s.split())
for a,b in enumerate(inputfile):
d = dict()
for c in inputfile[a]:
d.update({c[0:c.find(":")]: c[c.find(":")+1:]})
passports.append(d)
return passports
def countDigits(value):
count = 0
for c in value:
if c.isdigit():
count += 1
if not c.isdigit():
break
return count
def byr(value):
#byr (Birth Year) - four digits; at least 1920 and at most 2002.
if countDigits(value) == 4 and int(value) in range(1920,2003):
return True
return False
def iyr(value):
# iyr (Issue Year) - four digits; at least 2010 and at most 2020.
if countDigits(value) == 4 and int(value) in range(2010,2021):
return True
return False
def eyr(value):
#eyr (Expiration Year) - four digits; at least 2020 and at most 2030.
if countDigits(value) == 4 and int(value) in range(2020,2031):
return True
return False
def hgt(value):
# hgt (Height) - a number followed by either cm or in:
# If cm, the number must be at least 150 and at most 193.
# If in, the number must be at least 59 and at most 76.
number = int(''.join(list(filter(lambda c: c.isdigit(), value))))
measure = ''.join(list(filter(lambda c: c.isalpha(), value)))
if measure == 'cm' and number in range(150,194):
return True
elif measure == 'in' and number in range(59,77):
return True
return False
def hcl(value):
# hcl (Hair Color) - a # followed by exactly six characters 0-9 or a-f.
match = re.search(r'^#(?:[0-9a-fA-F]{3}){1,2}$', value)
if match:
return True
return False
def ecl(value):
# exactly one of: amb blu brn gry grn hzl oth.
if value in ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth']:
return True
return False
def pid(value):
# pid (Passport ID) - a nine-digit number, including leading zeroes.
if countDigits(value) == 9:
return True
return False
def validateData(key, value):
switcher = {
'byr': byr,
'iyr': iyr,
'eyr': eyr,
'hgt': hgt,
'hcl': hcl,
'ecl': ecl,
'pid': pid,
}
func = switcher.get(key, lambda x: False)
return func(value)
def checkValidity(passport):
mandatoryfields = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid']
for k in mandatoryfields:
if not k in passport:
return False
if not validateData(k, passport[k]):
return False
return True
main()