-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5.py
70 lines (55 loc) · 1.63 KB
/
day5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""for http://adventofcode.com/day/5
data was loaded by the following:
with open(file_path, 'r') as f:
data = f.readlines()
data = [s.strip() for s in data]
"""
def has_double_letter(s):
"""True if a letter is repeated in s, e.g. 'aa', 'aab', 'asdasdasdzz'"""
last_letter = None
for l in s:
if l == last_letter:
return True
last_letter = l
return False
def has_three_vowels(s):
"""True if s cotains three or more vowels"""
count = 0
for v in 'aeiou':
count += s.count(v)
return count >= 3
def has_bad_word(s):
"""True if a bad word is in s"""
bad = ('ab', 'cd', 'pq', 'xy')
return any(b in s for b in bad)
def part1(data):
"""Usage: part1(data)"""
nice_strings = 0
for s in data:
if (not has_bad_word(s)) and has_double_letter(s) and has_three_vowels(s):
nice_strings += 1
print 'Day 5 Part 1 answer: {}'.format(nice_strings)
def has_pairs(s):
"""True if a two letter pair is repeated in s"""
for i in range(len(s)):
t = s[i:i+2]
if len(t) < 2:
return False
if s.find(t, i+2) > -1:
return True
def has_sandwich(s):
"""True if a letter is repeated with a letter inbetween it in s
e.g. 'aaa', 'aba', 'efe', 'xux'
"""
for i, l in enumerate(s):
if i + 2 == len(s):
return False
if s[i+2] == l:
return True
def part2(data):
"""Usage: part2(data)"""
nice_strings = 0
for s in data:
if has_pairs(s) and has_sandwich(s):
nice_strings += 1
print 'Day 5 Part 2 answer: {}'.format(nice_strings)