-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16.py
58 lines (47 loc) · 1.33 KB
/
day16.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 aocd import get_data
import re
sue_pattern = re.compile(r"Sue (?P<id>\d+): (.+): (\d+), (.+): (\d+), (.+): (\d+)")
def is_okay(aunt, expectation):
for key in aunt:
if key == "id": continue
if aunt[key] != expectation[key]:
return False
return True
def is_okay_with_updated_retroencabulator(aunt, expectation):
for key in aunt:
if key == "id": continue
if key in ["cats", "trees"]:
if int(aunt[key]) <= int(expectation[key]):
return False
elif key in ["pomeranians", "goldfish"]:
if int(aunt[key]) >= int(expectation[key]):
return False
elif aunt[key] != expectation[key]:
return False
return True
def parse_expected(raw_expected):
return dict(map(lambda line: line.split(": "), raw_expected.split("\n")))
def parse_aunt(line):
match = sue_pattern.match(line)
return dict(zip(("id",)+match.groups()[1::2], match.groups()[::2]))
knowledge = get_data(day=16,year=2015)
raw_expected = """children: 3
cats: 7
samoyeds: 2
pomeranians: 3
akitas: 0
vizslas: 0
goldfish: 5
trees: 3
cars: 2
perfumes: 1"""
expected = parse_expected(raw_expected)
for raw_aunt in knowledge.split("\n"):
aunt = parse_aunt(raw_aunt)
if is_okay(aunt, expected):
print(aunt["id"])
break
for raw_aunt in knowledge.split("\n"):
aunt = parse_aunt(raw_aunt)
if is_okay_with_updated_retroencabulator(aunt, expected):
print(aunt["id"])