Juha-Matti Santala
Community Builder. Dreamer. Adventurer.

Advent of Code - 2015

This is a solution to Day 5 of Advent of Code 2015.

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.

Read input

from utils import read_input

strings = read_input(5)

Part 1

How many strings are nice?

Strings and rules! That's a good indicator that regular expressions could be the right tool for the job.

Python comes with regular expression library named re that we're using here.

For three vowel rule, I remove everything that is not a vowel (using character group ([]) and negation (^)) and then check if the length of the remaining is at least three.

For double letter rule, I use a backreference \1 to tell re that I want to find any character followed by itself. If such match is found, it's a nice string.

Finally, for no allowed strings rule I search for any of those four pairs and if none exist, it's nice.

If all these rules are passed, the string is nice and we use list comprehension to filter the list.

import re

def rule_three_vowels(string):
    vowel_pattern = r'[^aeiou]'
    return len(re.sub(vowel_pattern, '', string)) >= 3

def rule_twice(string):
    return re.search(r'(.)\1', string) is not None

def rule_no_allowed_strings(string):
    return re.search(r'ab|cd|pq|xy', string) is None

def is_nice(string):
    return rule_three_vowels(string) and rule_twice(string) and rule_no_allowed_strings(string)
    
def filter_nice(strings):
    return [string for string in strings if is_nice(string)]


result = len(filter_nice(strings))
print(f'Solution: {result}')
assert result == 238

Part 2

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?

Regular expressions are awesome for simple problems. These could probably be solved with regex too but the result would be something very difficult to read and understand so it's important to know when to drop it and use simpler and easier to read structures.

For double pair, we create all possible pairs (using pairwise that came in Python 3.10) and then see if they are included in the original string at least twice.

For repeat with between, we use zip to create trios. The idea is exactly the same as with pairwise but that only supports two-item windows and we need a three-item moving window.

For any trio, if first and third letter are the same, string is nice.

import re
from itertools import pairwise


def rule_double_pair(string):
    for pair in pairwise(string):
        if string.count(''.join(pair)) >= 2:
            return True
        
    return False

def rule_repeat_with_between(string):
    trios = zip(string, string[1:], string[2:])
    for first, _, third in trios:
        if first == third:
            return True
    return False
    
def new_is_nice(string):
    return rule_double_pair(string) and rule_repeat_with_between(string)


def filter_new_nice(strings):
    return [string for string in strings if new_is_nice(string)]


result = len(filter_new_nice(strings))
print(f'Solution: {result}')
assert result == 69