blob: 45cc6d338110d4ae3b0fa62a044334615b851510 (
plain) (
tree)
|
|
#!/usr/bin/env python
from collections import defaultdict
import re
target = {
'children': 3,
'cats': 7,
'samoyeds': 2,
'pomeranians': 3,
'akitas': 0,
'vizslas': 0,
'goldfish': 5,
'trees': 3,
'cars': 2,
'perfumes': 1
}
info_re = re.compile(r'(\w+): (\d)')
# part 1
with open('day16.txt') as data:
for num, line in enumerate(data):
current = dict(map(lambda x: (x[0], int(x[1])), info_re.findall(line)))
if all(target[key] == current[key] for key in current):
print(num+1)
break
# part 2
def valid(curr):
for key in curr:
if key == 'cats' or key == 'trees':
yield curr[key] > target[key]
elif key == 'pomeranians' or key == 'goldfish':
yield curr[key] < target[key]
else:
yield curr[key] == target[key]
with open('day16.txt') as data:
for num, line in enumerate(data):
current = dict(map(lambda x: (x[0], int(x[1])), info_re.findall(line)))
if all(valid(current)):
print(num+1)
break
|