Day 5 - Advent of Code 2015

Working solutions for the day 5 puzzles.

Part One

""" day_05_01.py """

# usage: python3 day_05_01.py < input


import sys


def not_banned(text):
    """ does not contain """
    for pair in ['ab', 'cd', 'pq', 'xy']:
        if pair in text:
            return False
    return True


def vowels3(text):
    """ contains three vowels """
    vowels = list('aeiou')
    tally = 0
    for v in vowels:
        tally += text.count(v)
        if tally > 2:
            return True
    return False


def double_letter(text):
    """ contains an adjacent repeated character """
    textlist = list(text)
    letter1 = textlist.pop()
    while textlist:
        letter2 = textlist.pop()
        if letter1 == letter2:
            return True
        letter1 = letter2
    return False


counter = 0
for string in sys.stdin.read().splitlines():
    if not_banned(string) and vowels3(string) and double_letter(string):
        counter += 1
print(counter)

Part Two

""" day_05_02.py """

# usage: python3 day_05_02.py < input


import sys


def pair_twice_or_more(text):
    """ pair occurs two or more times """
    for i, _ in enumerate(text[:-3]):
        if text.find(text[i:i + 2], i + 2) != -1:
            return True
    return False


def repeat_1_apart(text):
    """ letter repeats with another between """
    for i, letter in enumerate(text[:-2]):
        if letter == text[i + 2]:
            return True
    return False


counter = 0
for string in sys.stdin.read().splitlines():
    if pair_twice_or_more(string) and repeat_1_apart(string):
        counter += 1
print(counter)