Skip to content

Commit

Permalink
2015 day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
onefastsnail committed Aug 11, 2024
1 parent 2fca79c commit 10d0d28
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
Empty file added 2015/solutions/day4/__init__.py
Empty file.
21 changes: 21 additions & 0 deletions 2015/solutions/day4/solution.py
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions 2015/solutions/day4/test_day.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 10d0d28

Please sign in to comment.