-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-4.py
67 lines (56 loc) · 1.65 KB
/
day-4.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
import re
input = open('inputs/' + __file__.split('\\')[-1].split('.')[0] + '.txt', 'r')
input_passports = [[field.split(':') for field in re.split(' |\n', passport)] for passport in input.read().split('\n\n')]
# part 1
fields = [
'byr',
'iyr',
'eyr',
'hgt',
'hcl',
'ecl',
'pid',
]
valid = 0
for passport in input_passports:
if all(field in [item[0] for item in passport] for field in fields):
valid += 1
print('part 1: ' + str(valid))
# part 2
present_list = []
for passport in input_passports:
if all(field in [item[0] for item in passport] for field in fields):
if [''] in passport:
new = passport
new.remove([''])
present_list.append({field[0]: field[1] for field in new})
else:
present_list.append({field[0]: field[1] for field in passport})
ecl = [
'amb',
'blu',
'brn',
'gry',
'grn',
'hzl',
'oth'
]
valid = 0
for passport in present_list:
if not 1920<=int(passport['byr'])<=2002:
continue
if not 2010<=int(passport['iyr'])<=2020:
continue
if not 2020<=int(passport['eyr'])<=2030:
continue
if not ((passport['hgt'][-2:] == 'cm' and 150<=int(passport['hgt'][0:-2])<=193)
or (passport['hgt'][-2:] == 'in' and 59<=int(passport['hgt'][0:-2])<=76)):
continue
if not (re.match('#[a-f|0-9]{6}', passport['hcl']) is not None and len(passport['hcl']) == 7):
continue
if not (passport['ecl'] in ecl):
continue
if not (re.match('[0-9]{9}', passport['pid']) is not None and len(passport['pid']) == 9):
continue
valid += 1
print('part 2: ' + str(valid))