AdventOfCode/2015/05/solution.py

105 lines
2.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 isNaughty2(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
pairs = []
for i in range(0,len(inst)-1):
tmp = inst[i]+inst[i+1]
pairs.append(tmp)
#print(inst)
#print(pairs)
n_pairs = []
sdx = 0
while sdx < len(pairs)-2:
#for sdx in range(0,len(pairs)-1):
if pairs[sdx] != pairs[sdx+1]:
n_pairs.append(pairs[sdx])
if pairs[sdx] == pairs[sdx+1] and pairs[sdx] == pairs[sdx+2]:
n_pairs.append(pairs[sdx])
n_pairs.append(pairs[sdx+2])
sdx+=1
#print(n_pairs)
opt = [item for item in set(pairs) if n_pairs.count(item) > 1]
#print(opt)
if len(opt) > 0:
for i in range(0,len(inst)-2):
if inst[i] == inst[i+2]:
print(inst[i], inst[i+1],inst[i+2])
return False
return True
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):
#print('Nice')
count += 1
#input()
print(count)