day 3 - part 1

This commit is contained in:
Stanislas Jouffroy 2024-12-03 22:59:31 +01:00
parent 9d8f70f0a8
commit aa67f2a238
6 changed files with 98 additions and 0 deletions

View file

View file

@ -0,0 +1 @@
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))

View file

@ -0,0 +1,65 @@
from pathlib import Path
from aoc_2024.day3 import part1
def test_detect_mul_is_successful():
res = part1.detect_mul("mul(1,5)")[0]
assert res[0] == "1"
assert res[1] == "5"
def test_detect_mul_with_3_digits_is_successful():
assert part1.detect_mul("mul(123,567)")
def test_detect_mul_with_4_digits_fails():
assert not part1.detect_mul("mul(1234,5678)")
def test_detect_mul_with_space_fails():
assert not part1.detect_mul("mul(123 ,678)")
def test_detect_mul_without_mul_fails():
assert not part1.detect_mul("div(1,5)")
def test_detect_multiple_muls_is_successful():
res = part1.detect_mul(
"xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"
)
match1 = res[0]
assert match1[0] == "2"
assert match1[1] == "4"
match2 = res[1]
assert match2[0] == "5"
assert match2[1] == "5"
match3 = res[2]
assert match3[0] == "11"
assert match3[1] == "8"
match4 = res[3]
assert match4[0] == "8"
assert match4[1] == "5"
def test_compute_multiplication_from_mul():
assert part1.compute_multiplication("mul(1,5)") == 5
def test_compute_multiplication2_from_mul():
assert part1.compute_multiplication("mul(8,5)") == 40
def test_compute_multiplication2_from_multiple_muls():
assert (
part1.compute_multiplication(
"xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"
)
== 161
)
def test_main():
file = Path(__file__).parent / "test-data"
assert part1.main(file) == 161