From d1f20886fbdeeb2a692d0223f01678dfb7aa7909 Mon Sep 17 00:00:00 2001 From: Joscha Date: Tue, 6 Dec 2022 21:50:31 +0100 Subject: [PATCH] [py] Simplify 2015_05 --- py/aoc/y2015/d05.py | 62 ++++++++++----------------------------------- 1 file changed, 13 insertions(+), 49 deletions(-) diff --git a/py/aoc/y2015/d05.py b/py/aoc/y2015/d05.py index 34018f6..049c818 100644 --- a/py/aoc/y2015/d05.py +++ b/py/aoc/y2015/d05.py @@ -1,56 +1,20 @@ -def load_words(inputstr): - words = [] - for line in inputstr.splitlines(): - words.append(line) - return words +import re -# PART 1 +def part1_nice(word): + vowels = re.search(r"([aeiou].*){3}", word) + double = re.search(r"(.)\1", word) + forbidden = re.search(r"ab|cd|pq|xy", word) + return vowels and double and not forbidden -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 part2_nice(word): + pair = re.search(r"(..).*\1", word) + repeats = re.search(r"(.).\1", word) + return pair and repeats 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}") + words = inputstr.splitlines() + print(f"Part 1: {len(list(filter(part1_nice, words)))}") + print(f"Part 2: {len(list(filter(part2_nice, words)))}")