Skip to content

Commit

Permalink
Merge pull request #180 from Stegallo/2015
Browse files Browse the repository at this point in the history
2015
  • Loading branch information
Stegallo authored Nov 6, 2023
2 parents 32b9e93 + 56c26af commit a6f915a
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 41 deletions.
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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', 'pydantic']
# - repo: https://github.com/asottile/reorder_python_imports
# rev: v3.9.0
# hooks:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
| 01 | [puzzle][201501p]</br>[][201501] | | | | | | | |
| 02 | [puzzle][201502p]</br>[][201502] | | | | | | | |
| 03 | [puzzle][201503p]</br>[][201503] | | | | | | | |
| 04 | [puzzle][201504p]</br>[][201504] | | | | | | | |
| 05 | [puzzle][201505p]</br>[][201505] | | | | | | | |

[201501]: https://github.com/Stegallo/adventofcode/blob/master/y_2015/day1.py
[201501p]: https://adventofcode.com/2015/day/1
Expand Down
4 changes: 2 additions & 2 deletions common/aoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[flake8]
max-line-length = 88
ignore = E203,W503,W605,E741

[mypy]
plugins = pydantic.mypy
10 changes: 5 additions & 5 deletions tests/y_2015/test_2015_day4.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
72 changes: 72 additions & 0 deletions tests/y_2015/test_2015_day5.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
52 changes: 25 additions & 27 deletions y_2015/day5.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,36 @@
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:
return False
def __has_3_vovels(self) -> bool:
return sum(1 for i in self.text if i in "aeiou") >= 3

# 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:
break
if i == self.text[c + 1]:
twice_letter = True
break
if not twice_letter:
return False
return bool(twice_letter)

# not ab, cd, pq, or xy
if (
"ab" in self.text
or "cd" in self.text
or "pq" in self.text
or "xy" in self.text
):
return False
return True
def __has_no_forbidden_pairs(self) -> bool:
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 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:
Expand All @@ -47,21 +45,21 @@ def correct_nice(self) -> bool:
if self.text[c : c + 2] == self.text[j : j + 2]:
twice_pair = True
break
if not twice_pair:
return False
return bool(twice_pair)

# 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:
break
if self.text[c] == self.text[c + 2]:
letter_repeat = True
break
if not letter_repeat:
return False
return bool(letter_repeat)

return True
@property
def correct_nice(self) -> bool:
return self.__has_pair_twice() and self.__has_letter_repeats_with_one_between()


class Day(AoCDay):
Expand Down

0 comments on commit a6f915a

Please sign in to comment.