-
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 #176 from Stegallo/2015
day4
- Loading branch information
Showing
3 changed files
with
64 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from __future__ import annotations | ||
|
||
from unittest.mock import mock_open, patch, MagicMock | ||
|
||
from y_2015.day4 import Day | ||
|
||
with patch("builtins.open", mock_open(read_data="")): | ||
day = Day() | ||
|
||
|
||
def test_hash_logic(): | ||
day._input_data = [["abcdef"]] | ||
day._preprocess_input() | ||
assert day.hash_logic(5) == 609043 | ||
|
||
day._input_data = [["pqrstuv"]] | ||
day._preprocess_input() | ||
assert day.hash_logic(5) == 1048970 | ||
|
||
day._input_data = [["abcdef"]] | ||
day._preprocess_input() | ||
assert day.hash_logic(1) == 31 | ||
|
||
day._input_data = [["abcdef"]] | ||
day._preprocess_input() | ||
assert day.hash_logic(2) == 298 | ||
|
||
|
||
def test_calculate_1(): | ||
day.hash_logic = MagicMock() | ||
day._calculate_1() | ||
day.hash_logic.assert_called_once_with(5) | ||
|
||
|
||
def test_calculate_2(): | ||
day.hash_logic = MagicMock() | ||
day._calculate_2() | ||
day.hash_logic.assert_called_once_with(6) |
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,25 @@ | ||
import hashlib | ||
from common.aoc import AoCDay | ||
|
||
|
||
class Day(AoCDay): | ||
def __init__(self, test=0): | ||
super().__init__(__name__, test) | ||
|
||
def _preprocess_input(self) -> None: | ||
self.__secret_k = self._input_data[0][0] | ||
|
||
def hash_logic(self, num_zeros: int) -> int: | ||
i = self.__secret_k | ||
n = 0 | ||
while True: | ||
result = hashlib.md5(i.encode("ascii") + str(n).encode("ascii")).hexdigest() | ||
if result[:num_zeros] == "0" * num_zeros: | ||
return n | ||
n += 1 | ||
|
||
def _calculate_1(self) -> int: | ||
return self.hash_logic(5) | ||
|
||
def _calculate_2(self) -> int: | ||
return self.hash_logic(6) |