2015/day05: Complete!
ci/woodpecker/push/woodpecker Pipeline is pending
Details
ci/woodpecker/push/woodpecker Pipeline is pending
Details
parent
46828d7e83
commit
967da066ce
|
@ -0,0 +1,95 @@
|
|||
#!/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))
|
Binary file not shown.
|
@ -0,0 +1,13 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
from day05 import *
|
||||
|
||||
|
||||
def test_part1() -> None:
|
||||
with open("day05.txt", "r") as f:
|
||||
assert part1(f) == 238
|
||||
|
||||
|
||||
def test_part2() -> None:
|
||||
with open("day05.txt", "r") as f:
|
||||
assert part2(f) == 69
|
Loading…
Reference in New Issue