summary refs log blame commit diff stats
path: root/day5.py
blob: 25c6bc46a7fa2b97b8b983d99bfa6508f3bddf35 (plain) (tree)

































                                                                                       
#!/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])