Skip to content

Commit

Permalink
day4
Browse files Browse the repository at this point in the history
  • Loading branch information
Stegallo committed Nov 5, 2023
1 parent 12f766c commit e2495f6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/y_2015/test_2015_day3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from y_2015.day3 import Day

with patch("builtins.open", mock_open(read_data="1x1x1")):
with patch("builtins.open", mock_open(read_data=">")):
day = Day()


Expand Down
18 changes: 18 additions & 0 deletions tests/y_2015/test_2015_day4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from __future__ import annotations

from unittest.mock import mock_open, patch

from y_2015.day4 import Day

with patch("builtins.open", mock_open(read_data="")):
day = Day()


def test_calculate_1():
day._input_data = [["abcdef"]]
day._preprocess_input()
assert day._calculate_1() == 609043

day._input_data = [["pqrstuv"]]
day._preprocess_input()
assert day._calculate_1() == 1048970
25 changes: 25 additions & 0 deletions y_2015/day4.py
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)

0 comments on commit e2495f6

Please sign in to comment.