import re
with open('data-2015-05.txt', 'r') as f:
nice_list = f.read().splitlines()Advent of Code 2015 Day 5
— 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, according to some rules.
How many strings are nice?
I think we want some functions here?
def three_vowels(inp):
regex = '[aeiou].*[aeiou].*[aeiou]'
if re.search(regex, inp, re.IGNORECASE):
return True
else:
return False
def double_letter(inp):
regex = r'([a-z])\1'
if re.search(regex, inp, re.IGNORECASE):
return True
else:
return False
def forbidden(inp):
forb = ['ab', 'cd', 'pq', 'xy']
return all(f not in inp for f in forb)Call those functions
vowel_list = [three_vowels(x) for x in nice_list]
double_list = [double_letter(x) for x in nice_list]
forb_list = [forbidden(x) for x in nice_list]Then we want to zip to see how many are all True
sum(1 for v, d, f in zip(vowel_list, double_list, forb_list) if v and d and f)
# 258258
— Part Two —
Now we have some different rules to determine naughty and nice.
How many strings are nice under these new rules?
def double_pair(inp):
regex = r'([a-z]{2}).*\1'
if re.search(regex, inp, re.IGNORECASE):
return True
else:
return False
def repeat_gap(inp):
regex = r'([a-z])[a-z]\1'
if re.search(regex, inp, re.IGNORECASE):
return True
else:
return FalseCall the functions
pair_list = [double_pair(x) for x in nice_list]
rep_list = [repeat_gap(x) for x in nice_list]Make a count
sum(1 for p, r in zip(pair_list, rep_list) if p and r)
# 5353