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 --- day5.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 day5.py (limited to 'day5.py') diff --git a/day5.py b/day5.py new file mode 100644 index 0000000..25c6bc4 --- /dev/null +++ b/day5.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python + +from collections import Counter +from itertools import product + +with open('day5.txt') as data: + strings = [line.strip() for line in data] + +def isNice(string): + count_letters = Counter(string) + if sum(count_letters[x] for x in 'aeiou') < 3: + return False + if any(map(lambda x: x in string, ['ab', 'cd', 'pq', 'xy'])): + return False + if not any(map(lambda x: x*2 in string, 'abcdefghijklmnopqrstuvwxyz')): + return False + return True + +nice_counts = Counter(map(isNice, strings)) +print(nice_counts[True]) + +def isNice2(string): + def windows(size): + for i in range(len(string) - size + 1): + yield string[i:i+size] + if not any(window[0] == window[2] for window in windows(3)): + return False + pairs = map(lambda x: x[0] + x[1], product('abcdefghijklmnopqrstuvwxyz', repeat=2)) + if not any(map(lambda x: string.count(x) >= 2, pairs)): + return False + return True + +nice2_counts = Counter(map(isNice2, strings)) +print(nice2_counts[True]) -- cgit 1.4.1-2-gfad0