diff --git a/2015/day05.py b/2015/day05.py new file mode 100644 index 0000000..2e9ab70 --- /dev/null +++ b/2015/day05.py @@ -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)) diff --git a/2015/day05.txt b/2015/day05.txt new file mode 100644 index 0000000..150325b Binary files /dev/null and b/2015/day05.txt differ diff --git a/2015/day05_test.py b/2015/day05_test.py new file mode 100644 index 0000000..fc4c687 --- /dev/null +++ b/2015/day05_test.py @@ -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