-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday4.py
More file actions
138 lines (112 loc) · 4.03 KB
/
day4.py
File metadata and controls
138 lines (112 loc) · 4.03 KB
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
138
"""
Advent of Code, Day 4
"""
import sys
import re
# start reading first line into dict, when next line is '', start next dict
def get_pp_dicts(input):
alldictlist = []
adict= {}
for i,line in enumerate(input):
if not line == '':
print(line)
pairs = line.split(' ')
for pair in pairs:
key, value = pair.split(':')
adict[key] = value
elif line == '':
alldictlist.append(adict)
adict = {}
if i == len(input)-1:
alldictlist.append(adict)
return alldictlist
def count_valid_pp(pplist, needed):
valid_counter= 0
for passport in pplist:
all_there = all(field in passport for field in needed)
if all_there:
valid_counter = valid_counter + 1
return valid_counter
def get_valid_pp(pplist, needed):
valid_passports = []
for passport in pplist:
all_there = all(field in passport for field in needed)
if all_there:
valid_passports.append(passport)
return valid_passports
def check_val_year(value,digits,min,max):
valid = False
if len(str(value)) == digits and value >= min and value <= max:
#print('valid')
valid = True
return valid
def check_val_height(value,cmmin,cmmax,inmin,inmax):
valid = False
#could be only 2 digit number
if len(value) >= 4:
value_int = int(value[:-2])
if value.endswith('cm'):
if value_int >= cmmin and value_int <= cmmax:
valid = True
elif value.endswith('in'):
if value_int >= inmin and value_int <= inmax:
valid = True
return valid
def check_val_haircolor(value):
valid = False
# a # followed by exactly six characters 0-9 or a-f.
if value.startswith('#') and len(value)== 7:
regex1 = re.compile('[a-f]')
regex2 = re.compile('[0-9]')
if regex1.search(value) or regex2.search(value):
#print('valid')
valid = True
return valid
def check_val_eyecolor(value):
valid = False
validlist = ['amb' ,'blu', 'brn', 'gry', 'grn','hzl','oth']
if value in validlist:
#print('valid')
valid = True
return valid
def check_val_pid(value):
valid = False
if len(value) == 9:
#print('valid')
valid = True
return valid
def check_validity(valid_pp):
valid = True
valid_count = 0
for passport in valid_pp:
print('next')
checklist = [check_val_year(int(passport['byr']),4, 1920,2002),check_val_year(int(passport['iyr']),4, 2010,2020), check_val_year(int(passport['eyr']),4, 2020,2030), check_val_height(passport['hgt'],150,193,59,76), check_val_haircolor(passport['hcl']),check_val_eyecolor(passport['ecl']), check_val_pid(passport['pid'])]
if all(checklist):
valid_count = valid_count +1
return valid_count
"""
byr (Birth Year) - four digits; at least 1920 and at most 2002.
iyr (Issue Year) - four digits; at least 2010 and at most 2020.
eyr (Expiration Year) - four digits; at least 2020 and at most 2030.
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.
hcl (Hair Color) - a # followed by exactly six characters 0-9 or a-f.
ecl (Eye Color) - exactly one of: amb blu brn gry grn hzl oth.
pid (Passport ID) - a nine-digit number, including leading zeroes.
"""
if __name__ =="__main__":
inputtxt = sys.argv[1]
with open(inputtxt,'r') as inp:
input = inp.readlines()
input_wo_newline = [line.strip() for line in input]
print(input_wo_newline)
pp_dictlist = get_pp_dicts(input_wo_newline)
needed = ['byr', 'iyr','eyr', 'hgt', 'hcl','ecl', 'pid']
#optional = 'cid'
valid_pp_number = count_valid_pp(pp_dictlist, needed)
#PArt 1
print(valid_pp_number)
#Part 2
valid_passports = get_valid_pp(pp_dictlist, needed)
print(check_validity(valid_passports))