Skip to content

Commit

Permalink
Merge pull request #154 from GriceTurrble/feat/py-2024-day-3
Browse files Browse the repository at this point in the history
feaet: py 2024 day 03 complete
  • Loading branch information
GriceTurrble authored Dec 3, 2024
2 parents 58b378c + a56a5eb commit f06f043
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
6 changes: 6 additions & 0 deletions 2024/python/src/grice_py_aoc_2024/day03/inputs.txt

Large diffs are not rendered by default.

38 changes: 26 additions & 12 deletions 2024/python/src/grice_py_aoc_2024/day03/main.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
from __future__ import annotations

import re
import time
import typing
from pathlib import Path

if typing.TYPE_CHECKING:
from io import TextIOWrapper

FILE = Path(__file__).parent / "inputs.txt"


def part1(inputs: TextIOWrapper):
return "Not done yet!"
def part1(contents: str) -> int:
mul_pat = re.compile(r"mul\(\d+,\d+\)")
content = mul_pat.findall(contents)
result = 0
for bit in content:
left, right = map(int, bit[4:-1].split(","))
result += left * right
return result


def part2(inputs: TextIOWrapper):
return "Not done yet!"
def part2(contents: str) -> int:
mul_pat = re.compile(r"do\(\)|don't\(\)|mul\(\d+,\d+\)")
content = mul_pat.findall(contents)
result = 0
enabled = True
for bit in content:
if bit in ("do()", "don't()"):
enabled = bit == "do()"
continue
if enabled:
left, right = map(int, bit[4:-1].split(","))
result += left * right
return result


def main():
contents = FILE.read_text()

_start1 = time.perf_counter()
with open(FILE) as f:
result1 = part1(f)
result1 = part1(contents)
_delta1 = time.perf_counter() - _start1
print(f">> Part 1: {result1} ({_delta1:.6f}s)")

_start2 = time.perf_counter()
with open(FILE) as f:
result2 = part2(f)
result2 = part2(contents)
_delta2 = time.perf_counter() - _start2
print(f">> Part 2: {result2} ({_delta2:.6f}s)")

Expand Down
16 changes: 9 additions & 7 deletions 2024/python/src/grice_py_aoc_2024/day03/test_day03.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@

from .main import part1, part2

TEST_FILE = Path(__file__).parent / "test_inputs.txt"
RESULT_PART_1 = "Now it's done!"
RESULT_PART_2 = "Now it's done!"
DIR = Path(__file__).parent
TEST_FILE_P1 = DIR / "test_inputs_p1.txt"
TEST_FILE_P2 = DIR / "test_inputs_p2.txt"
RESULT_PART_1 = 161
RESULT_PART_2 = 48


def test_part1():
with open(TEST_FILE) as f:
result = part1(f)
contents = TEST_FILE_P1.read_text()
result = part1(contents)
assert result == RESULT_PART_1


def test_part2():
with open(TEST_FILE) as f:
result = part2(f)
contents = TEST_FILE_P2.read_text()
result = part2(contents)
assert result == RESULT_PART_2
Empty file.
1 change: 1 addition & 0 deletions 2024/python/src/grice_py_aoc_2024/day03/test_inputs_p1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
1 change: 1 addition & 0 deletions 2024/python/src/grice_py_aoc_2024/day03/test_inputs_p2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))

0 comments on commit f06f043

Please sign in to comment.