-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday16p2.py
More file actions
executable file
·74 lines (59 loc) · 1.7 KB
/
day16p2.py
File metadata and controls
executable file
·74 lines (59 loc) · 1.7 KB
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
#!/usr/bin/env python
import re
with open('input/day16.txt') as fh:
data = fh.read().rstrip('\n').split('\n')
pat = re.compile(
'Sue (\d+): (\w+): (\d+), (\w+): (\d+), (\w+): (\d+)'
)
results = {
'children': 3,
'cats': 7,
'samoyeds': 2,
'pomeranians': 3,
'akitas': 0,
'vizslas': 0,
'goldfish': 5,
'trees': 3,
'cars': 2,
'perfumes': 1
}
greater = ['cats', 'trees']
fewer = ['pomeranians', 'goldfish']
aunt_sues = dict()
for line in data:
m = re.search(pat, line)
if not m:
raise RuntimeError('Bad line!')
sue_number = int(m.group(1))
memories = dict()
memories[m.group(2)] = int(m.group(3))
memories[m.group(4)] = int(m.group(5))
memories[m.group(6)] = int(m.group(7))
for item in results:
if item not in memories:
memories[item] = -1
aunt_sues[sue_number] = memories
for item, value in results.iteritems():
for sue_number, memories in aunt_sues.iteritems():
if memories is None:
continue
if memories[item] == -1:
continue
if item in greater and not (memories[item] > value):
aunt_sues[sue_number] = None # Not the one!
continue
if item in fewer and not (memories[item] < value):
aunt_sues[sue_number] = None
continue
if (item not in greater and item not in fewer
and memories[item] != value):
aunt_sues[sue_number] = None
continue
remaining_sue = [
sue_number for sue_number in aunt_sues
if aunt_sues[sue_number] is not None
]
if len(remaining_sue) > 1:
print 'Too many Sues left: %s!' % len(remaining_sue)
else:
print remaining_sue[0]