-
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 #201 from Stegallo/2015
2015
- Loading branch information
Showing
4 changed files
with
88 additions
and
23 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 |
---|---|---|
@@ -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) |
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,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)) |