96 lines
1.9 KiB
Python
96 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
|
|
|
|
from typing import TextIO
|
|
|
|
|
|
def nice_or_naughty(string: str) -> int:
|
|
"""0 for naughty and 1 for nice"""
|
|
# count vowels
|
|
cv = (
|
|
string.count("a")
|
|
+ string.count("e")
|
|
+ string.count("i")
|
|
+ string.count("o")
|
|
+ string.count("u")
|
|
)
|
|
|
|
if cv < 3:
|
|
return 0
|
|
|
|
# twice in a row
|
|
dbl = 0
|
|
for ch in range(0, len(string)):
|
|
if len(string) > ch + 1 and string[ch] == string[ch + 1]:
|
|
dbl += 1
|
|
|
|
if dbl == 0:
|
|
return 0
|
|
|
|
# naughty strings
|
|
n = ("ab", "cd", "pq", "xy")
|
|
for x in n:
|
|
if x in string:
|
|
return 0
|
|
|
|
return 1
|
|
|
|
|
|
def nn2(string: str) -> int:
|
|
# first
|
|
a = 0 # we can only have 0 or 1, because of the return
|
|
# not anymore b/c it's BOTH, but whatever
|
|
|
|
for c in range(0, len(string) - 1):
|
|
pair = string[c : c + 2]
|
|
# print("pair is", pair)
|
|
|
|
for d in range(c + 2, len(string) - 1): # , len(string) - 2):
|
|
# print("trying to match", pair, string[d : d + 2])
|
|
if d + 1 < len(string) and string[d : d + 2] == pair:
|
|
# print("yay")
|
|
a += 1
|
|
break
|
|
else: # ?? huuhhh? https://stackoverflow.com/questions/653509/breaking-out-of-nested-loops
|
|
continue
|
|
|
|
break
|
|
|
|
# second
|
|
b = 0
|
|
|
|
for c in range(0, len(string) - 2):
|
|
if string[c] == string[c + 2]:
|
|
b += 1
|
|
break
|
|
|
|
if a == 1 and b == 1:
|
|
return 1
|
|
return 0
|
|
|
|
|
|
def part1(file: TextIO) -> int:
|
|
count = 0
|
|
|
|
for string in file:
|
|
count += nice_or_naughty(string.strip())
|
|
|
|
return count
|
|
|
|
|
|
def part2(file: TextIO) -> int:
|
|
count = 0
|
|
|
|
for string in file:
|
|
count += nn2(string.strip())
|
|
|
|
return count
|
|
|
|
|
|
if __name__ == "__main__":
|
|
with open("day05.txt", "r") as f:
|
|
print(part1(f))
|
|
|
|
with open("day05.txt", "r") as f:
|
|
print(part2(f))
|