From e2495f6ac6c96ae7eee03cfcecf7e7729c1c7e40 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sat, 4 Nov 2023 21:15:13 -0700 Subject: [PATCH] day4 --- tests/y_2015/test_2015_day3.py | 2 +- tests/y_2015/test_2015_day4.py | 18 ++++++++++++++++++ y_2015/day4.py | 25 +++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/y_2015/test_2015_day4.py create mode 100644 y_2015/day4.py diff --git a/tests/y_2015/test_2015_day3.py b/tests/y_2015/test_2015_day3.py index 2226562..d65babd 100644 --- a/tests/y_2015/test_2015_day3.py +++ b/tests/y_2015/test_2015_day3.py @@ -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() diff --git a/tests/y_2015/test_2015_day4.py b/tests/y_2015/test_2015_day4.py new file mode 100644 index 0000000..7e24792 --- /dev/null +++ b/tests/y_2015/test_2015_day4.py @@ -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 diff --git a/y_2015/day4.py b/y_2015/day4.py new file mode 100644 index 0000000..41a28b5 --- /dev/null +++ b/y_2015/day4.py @@ -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)