From e7085453864431ace3ad8f3123b259ed0829ae74 Mon Sep 17 00:00:00 2001 From: Brian Chu Date: Thu, 30 Dec 2021 15:11:21 -0800 Subject: all solutions for 2015 --- day16.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 day16.py (limited to 'day16.py') 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 -- cgit 1.4.1-2-gfad0