-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
executable file
·66 lines (53 loc) · 1.38 KB
/
run.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
#! /usr/bin/env python3
import re
def load_passports(filename):
with open(filename, 'r') as f:
p = {}
for line in f:
line = line.strip()
if line == '':
if len(p):
yield p
p = {}
continue
for t in line.split():
key, value = t.split(':')
p[key] = value
if len(p):
yield p
# Part One
total_valid = 0
for p in load_passports('input.txt'):
def valid(p):
required_keys = ( 'byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid' )
for key in required_keys:
if key not in p:
return False
return True
total_valid += valid(p)
print(total_valid)
# Part Two
total_valid = 0
for p in load_passports('input.txt'):
def valid(p):
constraints = {
'byr': (r'^(\d{4})$', lambda g: 1920 <= int(g[0]) <= 2002),
'iyr': (r'^(\d{4})$', lambda g: 2010 <= int(g[0]) <= 2020),
'eyr': (r'^(\d{4})$', lambda g: 2020 <= int(g[0]) <= 2030),
'hgt': (r'^(\d+)(in|cm)$', lambda g: {'in': 59, 'cm': 150}[g[1]] <= int(g[0]) <= {'in': 76, 'cm': 193}[g[1]]),
'hcl': (r'^#[\da-f]{6}$', None),
'ecl': (r'^(amb|blu|brn|gry|grn|hzl|oth)$', None),
'pid': (r'^\d{9}$', None),
}
for field, (r, checker) in constraints.items():
if field not in p:
return False
m = re.match(r, p[field])
if not m:
return False
if checker is not None:
if not checker(m.groups()):
return False
return True
total_valid += valid(p)
print(total_valid)