AdventOfCode/2015/05/solution.py

72 lines
1.7 KiB
Python

#!/bin/python3
import sys,re
from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int
input_f = 'input'
part = 2
#########################################
# #
# Part 1 #
# #
#########################################
def isNaughty(inst):
bad_words = ['ab','cd','pq','xy']
if any(w in inst for w in bad_words):
return True
if sum(inst.lower().count(v) for v in "aeiou") < 3:
return True
for i in range(0,len(inst)-1):
if inst[i] == inst[i+1]:
return False
return True
instructions = []
if part == 1:
count = 0
with open(input_f) as file:
for line in file:
instructions.append(line.rstrip())
for inst in instructions:
if not isNaughty(inst):
count += 1
print(count)
#########################################
# #
# Part 2 #
# #
#########################################
def find_non_overlapping_pairs(string):
pattern = r'(..).*?\1'
matches = re.findall(pattern, string)
return list(set(matches))
def isNaughty2(inst):
for i in range(0,len(inst)-2):
if inst[i] == inst[i+2] and len(find_non_overlapping_pairs(inst)) > 0:
return False
return True
if part == 2:
instructions = []
count = 0
with open(input_f) as file:
for line in file:
instructions.append(line.rstrip())
for inst in instructions:
if not isNaughty2(inst):
count += 1
print(count)