#!/usr/bin/env python # coding: utf-8 # In[10]: def nice(s): prev_ch = None vowels = 0 doubles = 0 disallowed_strings = 0 for ch in s: if ch in ('a', 'e', 'i', 'o', 'u'): vowels += 1 if ch == prev_ch: doubles += 1 if prev_ch and prev_ch + ch in ('ab', 'cd', 'pq', 'xy'): disallowed_strings += 1 prev_ch = ch return vowels >= 3 and \ doubles >= 1 and \ disallowed_strings == 0 # In[11]: assert nice('ugknbfddgicrmopn') == True assert nice('aaa') == True assert nice('jchzalrnumimnmhp') == False assert nice('haegwjzuvuyypxyu') == False assert nice('dvszwmarrgswjxmb') == False # In[12]: import import_ipynb import helper s = helper.read_file('2015_5.txt') # In[13]: sum(map(nice, s.splitlines())) # In[21]: def nice2(s): doubles = 0 repeating = 0 for i in range(len(s)): if i > len(s)-3: break if s[i] == s[i+2]: repeating += 1 for j in range(i+2, len(s)-1): if s[i] == s[j] and s[i+1] == s[j+1]: doubles += 1 return doubles >= 1 and repeating >= 1 # In[22]: assert nice2('qjhvhtzxzqqjkmpb') == True assert nice2('xxyxx') == True assert nice2('uurcxstgmygtbstg') == False assert nice2('ieodomkazucvgmuy') == False # In[23]: sum(map(nice2, s.splitlines())) # In[ ]: