# Matthew Ryan Dillon
# github.com/thermokarst
#
# --- Day 5: Doesn't He Have Intern-Elves For This? ---
#
# Santa needs help figuring out which strings in his text file are naughty or
# nice.
#
# A nice string is one with all of the following properties:
#
# - It contains at least three vowels (aeiou only), like aei, xazegov, or
#   aeiouaeiouaeiou.
# - It contains at least one letter that appears twice in a row, like xx, abcdde
#   (dd), or aabbccdd (aa, bb, cc, or dd).
# - It does not contain the strings ab, cd, pq, or xy, even if they are part of
#   one of the other requirements.
#
# For example:
#
# - ugknbfddgicrmopn is nice because it has at least three vowels (u...i...o...),
#   a double letter (...dd...), and none of the disallowed substrings.
# - aaa is nice because it has at least three vowels and a double letter, even
#   though the letters used by different rules overlap.
# - jchzalrnumimnmhp is naughty because it has no double letter.
# - haegwjzuvuyypxyu is naughty because it contains the string xy.
# - dvszwmarrgswjxmb is naughty because it contains only one vowel.
#
# How many strings are nice?
#
# --- Part Two ---
#
# Realizing the error of his ways, Santa has switched to a better model of
# determining whether a string is naughty or nice. None of the old rules apply,
# as they are all clearly ridiculous.
#
# Now, a nice string is one with all of the following properties:
#
# - It contains a pair of any two letters that appears at least twice in the
#   string without overlapping, like xyxy (xy) or aabcdefgaa (aa), but not like
#   aaa (aa, but it overlaps).
# - It contains at least one letter which repeats with exactly one letter between
#   them, like xyx, abcdefeghi (efe), or even aaa.
#
# For example:
#
# - qjhvhtzxzqqjkmpb is nice because is has a pair that appears twice (qj) and a
#   letter that repeats with exactly one letter between them (zxz).
# - xxyxx is nice because it has a pair that appears twice and a letter that
#   repeats with one between, even though the letters used by each rule overlap.
# - uurcxstgmygtbstg is naughty because it has a pair (tg) but no repeat with a
#   single letter between them.
# - ieodomkazucvgmuy is naughty because it has a repeating letter with one
#   between (odo), but no pair that appears twice.
#
# How many strings are nice under these new rules?

def vowel_count(value):
    count = 0
    vowel_list = list('aeiou')
    for v in value:
        if v in vowel_list:
            count += 1
    return count

def enough_vowels(value):
    return vowel_count(value) >= 3

def no_banned(value):
    for banned_string in ['ab', 'cd', 'pq', 'xy']:
        if banned_string in value:
            return False
    return True

def has_repeats(value, skip):
    i = 0
    while i < len(value) - 1 - skip:
        if value[i] == value[i + 1 + skip]:
            return True
        i += 1
    return False

def is_nice_old_rules(value):
    return no_banned(value) and enough_vowels(value) and has_repeats(value, 0)

def has_repeating_pair(value):
    i = 0
    while i < len(value) - 1:
        if len(value.split(value[i:i + 2])) > 2:
            return True
        i += 1
    return False

def is_nice_new_rules(value):
    return has_repeating_pair(value) and has_repeats(value, 1)

for x in ['qjhvhtzxzqqjkmpb', 'xxyxx', 'uurcxstgmygtbstg', 'ieodomkazucvgmuy']:
    print(x, is_nice_new_rules(x))

nice_count_old_rules = 0
nice_count_new_rules = 0

with open("input/day05a.txt") as f:
    for line in f:
        if is_nice_old_rules(line):
            nice_count_old_rules += 1
        if is_nice_new_rules(line):
            nice_count_new_rules += 1

print("nice count (old rules):", nice_count_old_rules)
print("nice count (new rules):", nice_count_new_rules)
