-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from Stegallo/2015
day5
- Loading branch information
Showing
6 changed files
with
165 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
requests | ||
pydantic | ||
requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pytest | ||
isort | ||
black | ||
flake8 | ||
isort | ||
pre-commit | ||
pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from __future__ import annotations | ||
|
||
from unittest.mock import mock_open, patch | ||
|
||
from pydantic.dataclasses import dataclass | ||
|
||
from y_2015.day5 import Day, NiceString | ||
|
||
with patch("builtins.open", mock_open(read_data="")): | ||
day = Day() | ||
|
||
|
||
def test_NiceString_nice(): | ||
assert NiceString("ugknbfddgicrmopn").nice is True | ||
assert NiceString("aaa").nice is True | ||
assert NiceString("jchzalrnumimnmhp").nice is False | ||
assert NiceString("haegwjzuvuyypxyu").nice is False | ||
assert NiceString("dvszwmarrgswjxmb").nice is False | ||
|
||
|
||
def test_NiceString_correct_nice(): | ||
assert NiceString("qjhvhtzxzqqjkmpb").correct_nice is True | ||
assert NiceString("xxyxx").correct_nice is True | ||
assert NiceString("aaaa").correct_nice is True | ||
assert NiceString("uurcxstgmygtbstg").correct_nice is False | ||
assert NiceString("ieodomkazucvgmuy").correct_nice is False | ||
|
||
|
||
def test_calculate_1(): | ||
@dataclass | ||
class MockNiceString: | ||
nice: bool | ||
|
||
day._Day__input_data = [] | ||
assert day._calculate_1() == 0 | ||
|
||
day._Day__input_data = [MockNiceString(True)] | ||
assert day._calculate_1() == 1 | ||
|
||
day._Day__input_data = [MockNiceString(True), MockNiceString(False)] | ||
assert day._calculate_1() == 1 | ||
|
||
day._Day__input_data = [MockNiceString(True), MockNiceString(True)] | ||
assert day._calculate_1() == 2 | ||
|
||
day._Day__input_data = [ | ||
MockNiceString(True), | ||
MockNiceString(True), | ||
MockNiceString(False), | ||
MockNiceString(False), | ||
] | ||
assert day._calculate_1() == 2 | ||
|
||
|
||
def test_calculate_2(): | ||
@dataclass | ||
class MockNiceString: | ||
correct_nice: bool | ||
|
||
day._Day__input_data = [MockNiceString(True), MockNiceString(False)] | ||
assert day._calculate_2() == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from pydantic.dataclasses import dataclass | ||
|
||
from common.aoc import AoCDay | ||
|
||
|
||
@dataclass | ||
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 | ||
|
||
# one letter twice | ||
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 | ||
|
||
# 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 | ||
|
||
@property | ||
def correct_nice(self) -> bool: | ||
# pair of any two letters that appears at least twice in the string | ||
# without overlapping | ||
twice_pair = False | ||
for c, i in enumerate(self.text): | ||
if c >= len(self.text) - 2: | ||
break | ||
for j in range(c + 2, len(self.text) - 1): | ||
if self.text[c : c + 2] == self.text[j : j + 2]: | ||
twice_pair = True | ||
break | ||
if not twice_pair: | ||
return False | ||
|
||
# one letter which repeats with exactly one letter between them | ||
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 True | ||
|
||
|
||
class Day(AoCDay): | ||
def __init__(self, test=0): | ||
super().__init__(__name__, test) | ||
|
||
def _preprocess_input(self) -> None: | ||
self.__input_data = [NiceString(i) for i in self._input_data[0]] | ||
|
||
def _calculate_1(self) -> int: | ||
return sum(int(i.nice) for i in self.__input_data) | ||
|
||
def _calculate_2(self) -> int: | ||
return sum(int(i.correct_nice) for i in self.__input_data) |