diff options
author | Brian Chu <brianmchu42@gmail.com> | 2021-12-30 15:11:21 -0800 |
---|---|---|
committer | Brian Chu <brianmchu42@gmail.com> | 2021-12-30 15:11:21 -0800 |
commit | e7085453864431ace3ad8f3123b259ed0829ae74 (patch) | |
tree | 2ef1fbb0e9d02fc934b5e09d96dd187f3e371ea6 /day16.py | |
download | AdventOfCode2015-main.tar.gz |
all solutions for 2015 main
Diffstat (limited to 'day16.py')
-rw-r--r-- | day16.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/day16.py b/day16.py new file mode 100644 index 0000000..45cc6d3 --- /dev/null +++ b/day16.py @@ -0,0 +1,44 @@ +#!/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 |