Skip to content

Commit

Permalink
Merge pull request #201 from Stegallo/2015
Browse files Browse the repository at this point in the history
2015
  • Loading branch information
Stegallo authored Nov 13, 2023
2 parents 5547c7d + a6724fe commit d5b632e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 23 deletions.
36 changes: 13 additions & 23 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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']
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 32 additions & 0 deletions tests/y_2015/test_2015_day10.py
Original file line number Diff line number Diff line change
@@ -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)
39 changes: 39 additions & 0 deletions y_2015/day10.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit d5b632e

Please sign in to comment.