From d028ee1bbf98cc881b36bfbda7896c02773b8d7a Mon Sep 17 00:00:00 2001 From: Joscha Date: Tue, 6 Dec 2022 20:10:47 +0100 Subject: [PATCH] [py] Port 2015_05 --- py/2015/05/solve.py | 54 ------------------------------------------- py/aoc/__init__.py | 3 ++- py/aoc/y2015/d05.py | 56 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 55 deletions(-) delete mode 100644 py/2015/05/solve.py create mode 100644 py/aoc/y2015/d05.py diff --git a/py/2015/05/solve.py b/py/2015/05/solve.py deleted file mode 100644 index c32e89d..0000000 --- a/py/2015/05/solve.py +++ /dev/null @@ -1,54 +0,0 @@ -import sys - -def load_words(filename): - words = [] - with open(filename, "r") as f: - for line in f: - words.append(line[:-1]) - return words - -# PART 1 - -def is_vowel(char): - return char in "aeiou" - -def has_double_letter(word): - # This would look nicer im haskell: - return count(zip(word, word[1:]), lambda x: x[0] == x[1]) > 0 - -def is_nice(word): - if "ab" in word or "cd" in word or "pq" in word or "xy" in word: - return False - if len(list(filter(is_vowel, word))) < 3: - return False - return has_double_letter(word) - -def count(what, function): - # This would also look nicer in haskell - return len(list(filter(function, what))) - -# PART 2 - -def has_pair(word): - for i in range(len(word)): - if word[i:i+2] in word[i+2:]: - return True - return False - -def has_repeat_letter(word): - return count(zip(word, word[2:]), lambda x: x[0] == x[1]) > 0 - -def is_nice_2(word): - return has_pair(word) and has_repeat_letter(word) - -def main(filename): - words = load_words(filename) - print(f"Solutions for {filename}") - amount = count(words, is_nice) - print(f"Part 1: {amount}") - amount_2 = count(words, is_nice_2) - print(f"Part 2: {amount_2}") - -if __name__ == "__main__": - for filename in sys.argv[1:]: - main(filename) diff --git a/py/aoc/__init__.py b/py/aoc/__init__.py index 930bb23..f604c33 100644 --- a/py/aoc/__init__.py +++ b/py/aoc/__init__.py @@ -2,7 +2,7 @@ import sys import argparse from pathlib import Path -from .y2015 import d01, d02, d03, d04 +from .y2015 import d01, d02, d03, d04, d05 from .y2016 import d01, d02, d03, d04, d05 from .y2017 import d01, d02, d03, d04, d05, d06, d07, d08, d09 from .y2018 import d01, d02, d03, d04, d05, d06, d07, d08, d09, d10, d11 @@ -15,6 +15,7 @@ DAYS = { "2015_02": y2015.d02.solve, "2015_03": y2015.d03.solve, "2015_04": y2015.d04.solve, + "2015_05": y2015.d05.solve, "2016_01": y2016.d01.solve, "2016_02": y2016.d02.solve, "2016_03": y2016.d03.solve, diff --git a/py/aoc/y2015/d05.py b/py/aoc/y2015/d05.py new file mode 100644 index 0000000..34018f6 --- /dev/null +++ b/py/aoc/y2015/d05.py @@ -0,0 +1,56 @@ +def load_words(inputstr): + words = [] + for line in inputstr.splitlines(): + words.append(line) + return words + + +# PART 1 + + +def is_vowel(char): + return char in "aeiou" + + +def has_double_letter(word): + # This would look nicer im haskell: + return count(zip(word, word[1:]), lambda x: x[0] == x[1]) > 0 + + +def is_nice(word): + if "ab" in word or "cd" in word or "pq" in word or "xy" in word: + return False + if len(list(filter(is_vowel, word))) < 3: + return False + return has_double_letter(word) + + +def count(what, function): + # This would also look nicer in haskell + return len(list(filter(function, what))) + + +# PART 2 + + +def has_pair(word): + for i in range(len(word)): + if word[i : i + 2] in word[i + 2 :]: + return True + return False + + +def has_repeat_letter(word): + return count(zip(word, word[2:]), lambda x: x[0] == x[1]) > 0 + + +def is_nice_2(word): + return has_pair(word) and has_repeat_letter(word) + + +def solve(inputstr): + words = load_words(inputstr) + amount = count(words, is_nice) + print(f"Part 1: {amount}") + amount_2 = count(words, is_nice_2) + print(f"Part 2: {amount_2}")