diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 41c325f..4140ff1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -24,34 +24,24 @@ repos: rev: 6.0.0 hooks: - id: flake8 - - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.6.1 + - repo: https://github.com/psf/black + rev: 23.11.0 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: - # - id: reorder-python-imports - # args: [--py37-plus] + - id: black - repo: https://github.com/asottile/add-trailing-comma rev: v2.3.0 hooks: - id: add-trailing-comma args: [--py36-plus] - - repo: https://github.com/asottile/pyupgrade - rev: v3.2.2 + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.1.5 hooks: - - id: pyupgrade - args: [--py37-plus] - - repo: https://github.com/pre-commit/mirrors-autopep8 - rev: v2.0.0 + - id: ruff + args: [ --fix ] + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v1.6.1 hooks: - - id: autopep8 - # - repo: https://github.com/Zac-HD/shed - # rev: 0.10.7 - # hooks: - # - id: shed - # types_or: [python, markdown, rst] + - id: mypy + verbose: true + args: [--show-error-codes, --explicit-package-bases] + additional_dependencies: ['types-requests', 'pydantic'] diff --git a/README.md b/README.md index 6ecd9dc..bd9ea16 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,10 @@ [201507p]: https://adventofcode.com/2015/day/7 [201508]: https://github.com/Stegallo/adventofcode/blob/master/y_2015/day8.py [201508p]: https://adventofcode.com/2015/day/8 +[201509]: https://github.com/Stegallo/adventofcode/blob/master/y_2015/day9.py +[201509p]: https://adventofcode.com/2015/day/9 +[201510]: https://github.com/Stegallo/adventofcode/blob/master/y_2015/day10.py +[201510p]: https://adventofcode.com/2015/day/10 ## to run the code diff --git a/tests/y_2015/test_2015_day10.py b/tests/y_2015/test_2015_day10.py new file mode 100644 index 0000000..431d6c8 --- /dev/null +++ b/tests/y_2015/test_2015_day10.py @@ -0,0 +1,32 @@ +from __future__ import annotations + +from unittest.mock import mock_open, patch, MagicMock + +from y_2015.day10 import Day + +with patch("builtins.open", mock_open(read_data="")): + day = Day() + + +def test_apply_times(): + assert day._apply_times("1", 1) == "11" + assert day._apply_times("11", 1) == "21" + + assert day._apply_times("1", 2) == "21" + assert day._apply_times("1", 3) == "1211" + assert day._apply_times("1", 4) == "111221" + assert day._apply_times("1", 5) == "312211" + + +def test_calculate_1(): + day._Day__input_data = ["1"] + day._apply_times = MagicMock() + day._calculate_1() + day._apply_times.assert_called_once_with(["1"], 40) + + +def test_calculate_2(): + day._Day__input_data = ["1"] + day._apply_times = MagicMock() + day._calculate_2() + day._apply_times.assert_called_once_with(["1"], 50) diff --git a/y_2015/day10.py b/y_2015/day10.py new file mode 100644 index 0000000..5d49e76 --- /dev/null +++ b/y_2015/day10.py @@ -0,0 +1,39 @@ +from common.aoc import AoCDay + + +class Day(AoCDay): + def __init__(self, test=0): + super().__init__(__name__, test) + + def _preprocess_input(self): + self.__input_data = self._input_data[0][0] + + def say_loud(self, x: str) -> str: + if len(x) == 1: + return f"1{x[0]}" + chunks = [] + chunk = [x[0]] + for c in range(len(x)): + if c == 0: + continue + + if x[c - 1] == x[c]: + chunk.append(x[c]) + else: + chunks.append(chunk) + chunk = [x[c]] + chunks.append(chunk) + k = [str(len(i)) + i[0] for i in chunks] + return "".join(k) + + def _apply_times(self, input: str, times: int) -> str: + result = input + for _ in range(times): + result = self.say_loud(result) + return result + + def _calculate_1(self): + return len(self._apply_times(self.__input_data, 40)) + + def _calculate_2(self): + return len(self._apply_times(self.__input_data, 50))