From d174fd7c6658a9ef9bb711a5f96f919718eff6b7 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sun, 5 Nov 2023 12:44:27 -0800 Subject: [PATCH 1/6] readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index abe2789..c521fe5 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ | 01 | [puzzle][201501p]
[✓][201501] | | | | | | | | | 02 | [puzzle][201502p]
[✓][201502] | | | | | | | | | 03 | [puzzle][201503p]
[✓][201503] | | | | | | | | +| 04 | [puzzle][201504p]
[✓][201504] | | | | | | | | +| 05 | [puzzle][201505p]
[✓][201505] | | | | | | | | [201501]: https://github.com/Stegallo/adventofcode/blob/master/y_2015/day1.py [201501p]: https://adventofcode.com/2015/day/1 From 00a45770d2c2c7a525579b3544bd77d70e0448bc Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sun, 5 Nov 2023 16:57:30 -0800 Subject: [PATCH 2/6] mypy --- .pre-commit-config.yaml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4c057aa..5e19833 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -24,13 +24,13 @@ repos: rev: 6.0.0 hooks: - id: flake8 - # - repo: https://github.com/pre-commit/mirrors-mypy - # rev: v1.6.1 - # hooks: - # - id: mypy - # verbose: true - # args: [--show-error-codes, --explicit-package-bases] - # additional_dependencies: ['types-requests'] + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v1.6.1 + hooks: + - id: mypy + verbose: true + args: [--show-error-codes, --explicit-package-bases] + additional_dependencies: ['types-requests'] # - repo: https://github.com/asottile/reorder_python_imports # rev: v3.9.0 # hooks: From 424eb6f29c361c1fc5e1563f5b5f980252db87bd Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sun, 5 Nov 2023 17:09:51 -0800 Subject: [PATCH 3/6] mypy --- .pre-commit-config.yaml | 2 +- common/aoc.py | 4 ++-- setup.cfg | 3 +++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5e19833..aadb8a3 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -30,7 +30,7 @@ repos: - id: mypy verbose: true args: [--show-error-codes, --explicit-package-bases] - additional_dependencies: ['types-requests'] + additional_dependencies: ['types-requests', 'pydantic'] # - repo: https://github.com/asottile/reorder_python_imports # rev: v3.9.0 # hooks: diff --git a/common/aoc.py b/common/aoc.py index 5e97d0b..82e6795 100644 --- a/common/aoc.py +++ b/common/aoc.py @@ -86,8 +86,8 @@ def from_path(path: str): TODO typehint """ day_info = DayInfo() - day_info.year = path.split(".")[0].replace("y_", "") - day_info.day = path.split(".")[1].replace("day", "") + day_info.year = int(path.split(".")[0].replace("y_", "")) + day_info.day = int(path.split(".")[1].replace("day", "")) return day_info diff --git a/setup.cfg b/setup.cfg index 74ea99e..941a1b7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,3 +1,6 @@ [flake8] max-line-length = 88 ignore = E203,W503,W605,E741 + +[mypy] +plugins = pydantic.mypy From d008c36a6678fce80e07eee19e2a128e4685072f Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sun, 5 Nov 2023 19:34:49 -0800 Subject: [PATCH 4/6] day5 --- y_2015/day5.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/y_2015/day5.py b/y_2015/day5.py index 810d68b..313d8be 100644 --- a/y_2015/day5.py +++ b/y_2015/day5.py @@ -7,14 +7,12 @@ class NiceString: text: str - @property - def nice(self) -> bool: - # 3 vovels - n_vovels = sum(1 for i in self.text if i in "aeiou") - if n_vovels < 3: + def __has_3_vovels(self) -> bool: + if sum(1 for i in self.text if i in "aeiou") < 3: return False + return True - # one letter twice + def __has_one_letter_twice(self) -> bool: twice_letter = False for c, i in enumerate(self.text): if c == len(self.text) - 1: @@ -24,8 +22,9 @@ def nice(self) -> bool: break if not twice_letter: return False + return True - # not ab, cd, pq, or xy + def __has_no_forbidden_pairs(self) -> bool: if ( "ab" in self.text or "cd" in self.text @@ -36,9 +35,14 @@ def nice(self) -> bool: return True @property - def correct_nice(self) -> bool: - # pair of any two letters that appears at least twice in the string - # without overlapping + def nice(self) -> bool: + return ( + self.__has_3_vovels() + and self.__has_one_letter_twice() + and self.__has_no_forbidden_pairs() + ) + + def __has_pair_twice(self) -> bool: twice_pair = False for c, i in enumerate(self.text): if c >= len(self.text) - 2: @@ -49,8 +53,9 @@ def correct_nice(self) -> bool: break if not twice_pair: return False + return True - # one letter which repeats with exactly one letter between them + def __has_letter_repeats_with_one_between(self) -> bool: letter_repeat = False for c, i in enumerate(self.text): if c >= len(self.text) - 2: @@ -60,9 +65,12 @@ def correct_nice(self) -> bool: break if not letter_repeat: return False - return True + @property + def correct_nice(self) -> bool: + return self.__has_pair_twice() and self.__has_letter_repeats_with_one_between() + class Day(AoCDay): def __init__(self, test=0): From 0564900e46fc3a2aeffc5fc154073047f135560e Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sun, 5 Nov 2023 20:11:36 -0800 Subject: [PATCH 5/6] test day4 day5 improve --- tests/y_2015/test_2015_day4.py | 10 ++--- tests/y_2015/test_2015_day5.py | 72 ++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/tests/y_2015/test_2015_day4.py b/tests/y_2015/test_2015_day4.py index cfcef75..fa4d8dc 100644 --- a/tests/y_2015/test_2015_day4.py +++ b/tests/y_2015/test_2015_day4.py @@ -11,19 +11,19 @@ def test_hash_logic(): day._input_data = [["abcdef"]] day._preprocess_input() - assert day.hash_logic(5) == 609043 + assert day.hash_logic(1) == 31 - day._input_data = [["pqrstuv"]] + day._input_data = [["abcdef"]] day._preprocess_input() - assert day.hash_logic(5) == 1048970 + assert day.hash_logic(2) == 298 day._input_data = [["abcdef"]] day._preprocess_input() - assert day.hash_logic(1) == 31 + assert day.hash_logic(3) == 3337 day._input_data = [["abcdef"]] day._preprocess_input() - assert day.hash_logic(2) == 298 + assert day.hash_logic(4) == 31556 def test_calculate_1(): diff --git a/tests/y_2015/test_2015_day5.py b/tests/y_2015/test_2015_day5.py index 38c3ee5..91b2c88 100644 --- a/tests/y_2015/test_2015_day5.py +++ b/tests/y_2015/test_2015_day5.py @@ -10,6 +10,43 @@ day = Day() +def test_NiceString__has_3_vovels(): + assert NiceString("")._NiceString__has_3_vovels() is False + assert NiceString("a")._NiceString__has_3_vovels() is False + assert NiceString("aa")._NiceString__has_3_vovels() is False + assert NiceString("aab")._NiceString__has_3_vovels() is False + assert NiceString("aaa")._NiceString__has_3_vovels() is True + + assert NiceString("ugknbfddgicrmopn")._NiceString__has_3_vovels() is True + assert NiceString("aaa")._NiceString__has_3_vovels() is True + assert NiceString("dvszwmarrgswjxmb")._NiceString__has_3_vovels() is False + + +def test_NiceString__has_one_letter_twice(): + assert NiceString("")._NiceString__has_one_letter_twice() is False + assert NiceString("a")._NiceString__has_one_letter_twice() is False + assert NiceString("aa")._NiceString__has_one_letter_twice() is True + assert NiceString("aba")._NiceString__has_one_letter_twice() is False + assert NiceString("abaa")._NiceString__has_one_letter_twice() is True + + assert NiceString("ugknbfddgicrmopn")._NiceString__has_one_letter_twice() is True + assert NiceString("aaa")._NiceString__has_one_letter_twice() is True + assert NiceString("jchzalrnumimnmhp")._NiceString__has_one_letter_twice() is False + + +def test_NiceString__has_no_forbidden_pairs(): + assert NiceString("")._NiceString__has_no_forbidden_pairs() is True + assert NiceString("ab")._NiceString__has_no_forbidden_pairs() is False + assert NiceString("cd")._NiceString__has_no_forbidden_pairs() is False + assert NiceString("pq")._NiceString__has_no_forbidden_pairs() is False + assert NiceString("xy")._NiceString__has_no_forbidden_pairs() is False + assert NiceString("acpx")._NiceString__has_no_forbidden_pairs() is True + + assert NiceString("ugknbfddgicrmopn")._NiceString__has_no_forbidden_pairs() is True + assert NiceString("aaa")._NiceString__has_no_forbidden_pairs() is True + assert NiceString("haegwjzuvuyypxyu")._NiceString__has_no_forbidden_pairs() is False + + def test_NiceString_nice(): assert NiceString("ugknbfddgicrmopn").nice is True assert NiceString("aaa").nice is True @@ -18,6 +55,41 @@ def test_NiceString_nice(): assert NiceString("dvszwmarrgswjxmb").nice is False +def test_NiceString__has_pair_twice(): + assert NiceString("xyxy")._NiceString__has_pair_twice() is True + assert NiceString("aabcdefgaa")._NiceString__has_pair_twice() is True + assert NiceString("aaa")._NiceString__has_pair_twice() is False + + assert NiceString("qjhvhtzxzqqjkmpb")._NiceString__has_pair_twice() is True + assert NiceString("xxyxx")._NiceString__has_pair_twice() is True + assert NiceString("ieodomkazucvgmuy")._NiceString__has_pair_twice() is False + + +def test_NiceString__has_letter_repeats_with_one_between(): + assert NiceString("xyx")._NiceString__has_letter_repeats_with_one_between() is True + assert ( + NiceString("abcdefeghi")._NiceString__has_letter_repeats_with_one_between() + is True + ) + assert NiceString("aaa")._NiceString__has_letter_repeats_with_one_between() is True + + assert ( + NiceString( + "qjhvhtzxzqqjkmpb", + )._NiceString__has_letter_repeats_with_one_between() + is True + ) + assert ( + NiceString("xxyxx")._NiceString__has_letter_repeats_with_one_between() is True + ) + assert ( + NiceString( + "uurcxstgmygtbstg", + )._NiceString__has_letter_repeats_with_one_between() + is False + ) + + def test_NiceString_correct_nice(): assert NiceString("qjhvhtzxzqqjkmpb").correct_nice is True assert NiceString("xxyxx").correct_nice is True From 849797c503618a05cccab06271a1b0d7e3735d20 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Mon, 6 Nov 2023 04:12:31 +0000 Subject: [PATCH 6/6] 'Refactored by Sourcery' --- y_2015/day5.py | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/y_2015/day5.py b/y_2015/day5.py index 313d8be..b5a26c3 100644 --- a/y_2015/day5.py +++ b/y_2015/day5.py @@ -8,9 +8,7 @@ class NiceString: text: str def __has_3_vovels(self) -> bool: - if sum(1 for i in self.text if i in "aeiou") < 3: - return False - return True + return sum(1 for i in self.text if i in "aeiou") >= 3 def __has_one_letter_twice(self) -> bool: twice_letter = False @@ -20,19 +18,15 @@ def __has_one_letter_twice(self) -> bool: if i == self.text[c + 1]: twice_letter = True break - if not twice_letter: - return False - return True + return bool(twice_letter) def __has_no_forbidden_pairs(self) -> bool: - if ( - "ab" in self.text - or "cd" in self.text - or "pq" in self.text - or "xy" in self.text - ): - return False - return True + return ( + "ab" not in self.text + and "cd" not in self.text + and "pq" not in self.text + and "xy" not in self.text + ) @property def nice(self) -> bool: @@ -51,9 +45,7 @@ def __has_pair_twice(self) -> bool: if self.text[c : c + 2] == self.text[j : j + 2]: twice_pair = True break - if not twice_pair: - return False - return True + return bool(twice_pair) def __has_letter_repeats_with_one_between(self) -> bool: letter_repeat = False @@ -63,9 +55,7 @@ def __has_letter_repeats_with_one_between(self) -> bool: if self.text[c] == self.text[c + 2]: letter_repeat = True break - if not letter_repeat: - return False - return True + return bool(letter_repeat) @property def correct_nice(self) -> bool: