-
Notifications
You must be signed in to change notification settings - Fork 0
/
day04.py
58 lines (40 loc) · 1.6 KB
/
day04.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
from pathlib import Path
import numpy as np
from collections import ChainMap
import re
# input_matrix = np.array([list(line) for line in lines])
file = ''
with open('input/' + Path(__file__).stem, "r") as text_file:
file = text_file.read()
lines = np.array([passport.replace('\n', ' ')
for passport in file.split('\n\n')])
lines = np.array(
[dict(ChainMap(*list(map(lambda elem: dict([elem.split(':')]), line.split(' ')))))
for line in lines]
)
required_keys = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid']
optional_keys = ['cid']
def validation_fn1(l): return all(key in l for key in required_keys)
def is_valid_height(height):
if(height.endswith('cm')):
h = int(height.replace('cm', ''))
return h >= 150 and h <= 193
elif(height.endswith('in')):
h = int(height.replace('in', ''))
return h >= 59 and h <= 76
return False
hair_color_pattern = re.compile("^#[a-f0-9]{6}$")
pid_pattern = re.compile("^[0-9]{9}$")
def validation_fn2(l):
return (all(key in l for key in required_keys) and
int(l['byr']) >= 1920 and int(l['byr']) <= 2002 and
int(l['iyr']) >= 2010 and int(l['iyr']) <= 2020 and
int(l['eyr']) >= 2020 and int(l['eyr']) <= 2030 and
is_valid_height(l['hgt']) and
hair_color_pattern.match(l['hcl']) and
l['ecl'] in ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'] and
pid_pattern.match(l['pid']))
def solve(fn):
return list(filter(fn, lines))
print(f'Part 1: {len(solve(validation_fn1))}')
print(f'Part 2: {len(solve(validation_fn2))}')