Day 5, Year 2015: Doesn't He Have Intern-Elves For This?
First read the problem description.
def nice(s):
= None
prev_ch = 0
vowels = 0
doubles = 0
disallowed_strings for ch in s:
if ch in ('a', 'e', 'i', 'o', 'u'):
+= 1
vowels
if ch == prev_ch:
+= 1
doubles
if prev_ch and prev_ch + ch in ('ab', 'cd', 'pq', 'xy'):
+= 1
disallowed_strings
= ch
prev_ch
return vowels >= 3 and \
>= 1 and \
doubles == 0 disallowed_strings
assert nice('ugknbfddgicrmopn') == True
assert nice('aaa') == True
assert nice('jchzalrnumimnmhp') == False
assert nice('haegwjzuvuyypxyu') == False
assert nice('dvszwmarrgswjxmb') == False
import import_ipynb
import helper
= helper.read_file('2015_5.txt') s
sum(map(nice, s.splitlines()))
255
def nice2(s):
= 0
doubles = 0
repeating
for i in range(len(s)):
if i > len(s)-3:
break
if s[i] == s[i+2]:
+= 1
repeating
for j in range(i+2, len(s)-1):
if s[i] == s[j] and s[i+1] == s[j+1]:
+= 1
doubles
return doubles >= 1 and repeating >= 1
assert nice2('qjhvhtzxzqqjkmpb') == True
assert nice2('xxyxx') == True
assert nice2('uurcxstgmygtbstg') == False
assert nice2('ieodomkazucvgmuy') == False
sum(map(nice2, s.splitlines()))
55
Source code of the solution(s):