Skip to content

Commit f06f043

Browse files
authored
Merge pull request #154 from GriceTurrble/feat/py-2024-day-3
feaet: py 2024 day 03 complete
2 parents 58b378c + a56a5eb commit f06f043

File tree

6 files changed

+43
-19
lines changed

6 files changed

+43
-19
lines changed

2024/python/src/grice_py_aoc_2024/day03/inputs.txt

+6
Large diffs are not rendered by default.

2024/python/src/grice_py_aoc_2024/day03/main.py

+26-12
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
11
from __future__ import annotations
22

3+
import re
34
import time
4-
import typing
55
from pathlib import Path
66

7-
if typing.TYPE_CHECKING:
8-
from io import TextIOWrapper
9-
107
FILE = Path(__file__).parent / "inputs.txt"
118

129

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

1619

17-
def part2(inputs: TextIOWrapper):
18-
return "Not done yet!"
20+
def part2(contents: str) -> int:
21+
mul_pat = re.compile(r"do\(\)|don't\(\)|mul\(\d+,\d+\)")
22+
content = mul_pat.findall(contents)
23+
result = 0
24+
enabled = True
25+
for bit in content:
26+
if bit in ("do()", "don't()"):
27+
enabled = bit == "do()"
28+
continue
29+
if enabled:
30+
left, right = map(int, bit[4:-1].split(","))
31+
result += left * right
32+
return result
1933

2034

2135
def main():
36+
contents = FILE.read_text()
37+
2238
_start1 = time.perf_counter()
23-
with open(FILE) as f:
24-
result1 = part1(f)
39+
result1 = part1(contents)
2540
_delta1 = time.perf_counter() - _start1
2641
print(f">> Part 1: {result1} ({_delta1:.6f}s)")
2742

2843
_start2 = time.perf_counter()
29-
with open(FILE) as f:
30-
result2 = part2(f)
44+
result2 = part2(contents)
3145
_delta2 = time.perf_counter() - _start2
3246
print(f">> Part 2: {result2} ({_delta2:.6f}s)")
3347

2024/python/src/grice_py_aoc_2024/day03/test_day03.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@
44

55
from .main import part1, part2
66

7-
TEST_FILE = Path(__file__).parent / "test_inputs.txt"
8-
RESULT_PART_1 = "Now it's done!"
9-
RESULT_PART_2 = "Now it's done!"
7+
DIR = Path(__file__).parent
8+
TEST_FILE_P1 = DIR / "test_inputs_p1.txt"
9+
TEST_FILE_P2 = DIR / "test_inputs_p2.txt"
10+
RESULT_PART_1 = 161
11+
RESULT_PART_2 = 48
1012

1113

1214
def test_part1():
13-
with open(TEST_FILE) as f:
14-
result = part1(f)
15+
contents = TEST_FILE_P1.read_text()
16+
result = part1(contents)
1517
assert result == RESULT_PART_1
1618

1719

1820
def test_part2():
19-
with open(TEST_FILE) as f:
20-
result = part2(f)
21+
contents = TEST_FILE_P2.read_text()
22+
result = part2(contents)
2123
assert result == RESULT_PART_2

2024/python/src/grice_py_aoc_2024/day03/test_inputs.txt

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))

0 commit comments

Comments
 (0)