From 10d0d28fcffd89424ec8ef0aae2d38cdd3414763 Mon Sep 17 00:00:00 2001 From: onefastsnail Date: Sun, 11 Aug 2024 18:10:32 +0300 Subject: [PATCH] 2015 day 4 --- 2015/solutions/day4/__init__.py | 0 2015/solutions/day4/solution.py | 21 +++++++++++++++++++++ 2015/solutions/day4/test_day.py | 16 ++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 2015/solutions/day4/__init__.py create mode 100644 2015/solutions/day4/solution.py create mode 100644 2015/solutions/day4/test_day.py diff --git a/2015/solutions/day4/__init__.py b/2015/solutions/day4/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/2015/solutions/day4/solution.py b/2015/solutions/day4/solution.py new file mode 100644 index 0000000..7ea0668 --- /dev/null +++ b/2015/solutions/day4/solution.py @@ -0,0 +1,21 @@ +import hashlib + + +def part_1(raw_input: str) -> int: + return find_with("00000", raw_input) + + +def part_2(raw_input: str) -> int: + return find_with("000000", raw_input) + + +def find_with(prefix: str, raw_input: str) -> int: + number = 1 + + while True: + result = hashlib.md5(str.encode(f'{raw_input}{number}')).hexdigest() + + if result.startswith(prefix): + return number + + number = number + 1 diff --git a/2015/solutions/day4/test_day.py b/2015/solutions/day4/test_day.py new file mode 100644 index 0000000..e9aae50 --- /dev/null +++ b/2015/solutions/day4/test_day.py @@ -0,0 +1,16 @@ +import pytest + +from solutions.day4.solution import part_1, part_2 + + +@pytest.mark.parametrize("test_input,expected", [("abcdef", 609043), ("pqrstuv", 1048970)]) +def test_part_1_examples(test_input, expected): + assert part_1(test_input) == expected, f'Should be [{expected}] from [{test_input}]' + + +def test_part_1(): + assert part_1("ckczppom") == 117946 + + +def test_part_2(): + assert part_2("ckczppom") == 3938038