-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path04.py
87 lines (63 loc) · 1.83 KB
/
04.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
import re
def parse_pspt(lines):
pspt = {}
for line in lines:
for item in line.split(' '):
key, value = item.split(':')
pspt[key] = value
return pspt
def read_pspts():
result = []
cur = []
for line in open('input.txt'):
line = line.strip()
if not line:
result.append(parse_pspt(cur))
cur = []
continue
cur.append(line)
result.append(parse_pspt(cur))
return result
def is_valid(p):
expected = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid']
for e in expected:
if e not in p:
return False
return True
def star1():
pspts = read_pspts()
print(sum(1 for p in pspts if is_valid(p)))
def is_valid_number(s, gte, lte):
try:
x = int(s)
return gte <= x <= lte
except ValueError:
return False
def is_valid_hgt(s: str):
return s.endswith('cm') and is_valid_number(s[:-2], 150, 193) \
or s.endswith('in') and is_valid_number(s[:-2], 59, 76)
def is_valid_hcl(s):
return re.match('^#[0-9a-f]{6}$', s)
def is_valid_ecl(s):
return s in 'amb blu brn gry grn hzl oth'.split(' ')
def is_valid_pid(s):
return re.match('^[0-9]{9}$', s)
def is_valid2(p):
expected = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid']
for e in expected:
if e not in p:
return False
return is_valid_number(p['byr'], 1920, 2002) and \
is_valid_number(p['iyr'], 2010, 2020) and \
is_valid_number(p['eyr'], 2020, 2030) and \
is_valid_hgt(p['hgt']) and \
is_valid_hcl(p['hcl']) and \
is_valid_ecl(p['ecl']) and \
is_valid_pid(p['pid'])
def star2():
pspts = read_pspts()
print(sum(1 for p in pspts if is_valid2(p)))
print('Star 1:')
star1()
print('Star 2:')
star2()