2024-11-30 19:07:44 +01:00
|
|
|
#!/bin/python3
|
|
|
|
import sys,re
|
|
|
|
from pprint import pprint
|
|
|
|
sys.path.insert(0, '../../')
|
|
|
|
from fred import list2int
|
|
|
|
|
|
|
|
input_f = 'input'
|
|
|
|
|
2024-12-14 22:46:07 +01:00
|
|
|
part = 1
|
2024-11-30 19:07:44 +01:00
|
|
|
#########################################
|
|
|
|
# #
|
|
|
|
# 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 #
|
|
|
|
# #
|
|
|
|
#########################################
|
|
|
|
|
2024-11-30 19:20:09 +01:00
|
|
|
def find_non_overlapping_pairs(string):
|
|
|
|
pattern = r'(..).*?\1'
|
|
|
|
matches = re.findall(pattern, string)
|
|
|
|
return list(set(matches))
|
2024-11-30 19:07:44 +01:00
|
|
|
|
2024-11-30 19:20:09 +01:00
|
|
|
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
|
2024-11-30 19:07:44 +01:00
|
|
|
|
|
|
|
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)
|