You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
872 B
Python
37 lines
872 B
Python
3 years ago
|
with open("input") as f:
|
||
|
puzzle_input = [n.strip() for n in f.readlines()]
|
||
|
|
||
|
# puzzle_input = ["qjhvhtzxzqqjkmpb", "xxyxx", "uurcxstgmygtbstg", "ieodomkazucvgmuy"]
|
||
|
|
||
|
nice_words = 0
|
||
|
|
||
|
|
||
|
def has_duplicate_pair(word):
|
||
|
for idx_a in range(len(word) - 3):
|
||
|
for idx_b in range(idx_a + 2, len(word) - 1):
|
||
|
pair_1 = word[idx_a] + word[idx_a + 1]
|
||
|
pair_2 = word[idx_b] + word[idx_b + 1]
|
||
|
|
||
|
if pair_1 == pair_2:
|
||
|
return True
|
||
|
return False
|
||
|
|
||
|
|
||
|
def has_repeated_letter_with_one_between(word):
|
||
|
for idx, a in enumerate(word[:-2]):
|
||
|
b = word[idx + 2]
|
||
|
if a == b:
|
||
|
return True
|
||
|
return False
|
||
|
|
||
|
|
||
|
def is_nice_word(word):
|
||
|
return has_duplicate_pair(word) and has_repeated_letter_with_one_between(word)
|
||
|
|
||
|
|
||
|
for word in puzzle_input:
|
||
|
if is_nice_word(word):
|
||
|
nice_words += 1
|
||
|
|
||
|
print(nice_words)
|