-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16.py
63 lines (52 loc) · 1.58 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
59
60
61
62
63
import re
class SuePartA:
def __init__(self, idx, params):
self.idx = idx
self.params = {k: int(v) for k, v in params.items()}
def __eq__(self, other):
for name, value in other.params.items():
self_value = self.params.get(name, None)
if self_value is None:
continue
if value != self_value:
return False
return True
@classmethod
def from_line(cls, line):
match = re.match('Sue (\d+): (.*)', line)
idx, criterias = match.groups()
criterias = dict(re.findall('(\w+): (\d+)', line))
return cls(idx, criterias)
class SuePartB(SuePartA):
def __init__(self, idx, params):
super().__init__(idx, params)
def __eq__(self, other):
for name, value in other.params.items():
self_value = self.params.get(name, None)
if self_value is None:
continue
if name in ('cats', 'trees'):
if value > self_value:
return False
elif name in ('pomeranians', 'goldfish'):
if value < self_value:
return False
elif value != self_value:
return False
return True
SueClass = SuePartB
need = SueClass(-1, {
'children': 3,
'cats': 7,
'samoyeds': 2,
'pomeranians': 3,
'akitas': 0,
'vizslas': 0,
'goldfish': 5,
'trees': 3,
'cars': 2,
'perfumes': 1, })
for line in open('day16.txt'):
sue = SueClass.from_line(line)
if sue == need:
print(sue.idx)