Skip to content

Commit 9f91863

Browse files
feat: add 2023 day 2
1 parent 8027a93 commit 9f91863

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

src/adventofcode/year_2023/day_01_2023.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,48 @@ def get_calibration_value(line: str) -> int:
2828
return int(left + right)
2929

3030

31-
def get_calibration_values(lines: list[str]) -> int:
31+
def get_calibration_value_with_words(line: str) -> int:
32+
left: str | None = None
33+
left_idx: int | None = None
34+
right: str | None = None
35+
right_idx: int | None = None
36+
37+
for idx, char in enumerate(line):
38+
if char.isdigit():
39+
right = char
40+
right_idx = idx
41+
if left is None:
42+
left = char
43+
left_idx = idx
44+
45+
for word, value in DIGITS.items():
46+
if (lfind := line.find(word)) != -1 and (left_idx is None or lfind < left_idx):
47+
left = value
48+
left_idx = lfind
49+
if (rfind := line.rfind(word)) != -1 and (right_idx is None or rfind > right_idx):
50+
right = value
51+
right_idx = rfind
52+
53+
return int(left + right)
54+
55+
56+
def get_calibration_values(lines: list[str], *, with_words: bool) -> int:
3257
total: int = 0
3358

59+
if with_words:
60+
func = get_calibration_value_with_words
61+
else:
62+
func = get_calibration_value
63+
3464
for line in lines:
35-
total += get_calibration_value(line)
65+
total += func(line)
3666

3767
return total
3868

3969

4070
@register_solution(2023, 1, 1)
4171
def part_one(input_data: list[str]):
42-
answer = get_calibration_values(input_data)
72+
answer = get_calibration_values(input_data, with_words=False)
4373

4474
if not answer:
4575
raise SolutionNotFoundError(2023, 1, 1)
@@ -49,7 +79,7 @@ def part_one(input_data: list[str]):
4979

5080
@register_solution(2023, 1, 2)
5181
def part_two(input_data: list[str]):
52-
answer = ...
82+
answer = get_calibration_values(input_data, with_words=True)
5383

5484
if not answer:
5585
raise SolutionNotFoundError(2023, 1, 2)

tests/adventofcode/year_2023/test_day_01_2023.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

33
from adventofcode.year_2023.day_01_2023 import (part_two, part_one, get_calibration_value,
4+
get_calibration_value_with_words,
45
)
56

67
test_input = [
@@ -10,6 +11,16 @@
1011
"treb7uchet",
1112
]
1213

14+
test_input_2 = [
15+
"two1nine",
16+
"eightwothree",
17+
"abcone2threexyz",
18+
"xtwone3four",
19+
"4nineeightseven2",
20+
"zoneight234",
21+
"7pqrstsixteen",
22+
]
23+
1324

1425
@pytest.mark.parametrize(["line", "expected"], [
1526
("1abc2", 12),
@@ -21,9 +32,22 @@ def test_get_calibration_value(line, expected):
2132
assert get_calibration_value(line) == expected
2233

2334

35+
@pytest.mark.parametrize(["line", "expected"], [
36+
("two1nine", 29),
37+
("eightwothree", 83),
38+
("abcone2threexyz", 13),
39+
("xtwone3four", 24),
40+
("4nineeightseven2", 42),
41+
("zoneight234", 14),
42+
("7pqrstsixteen", 76),
43+
])
44+
def test_get_calibration_value_with_words(line, expected):
45+
assert get_calibration_value_with_words(line) == expected
46+
47+
2448
def test_part_one():
2549
assert part_one(test_input) == 142
2650

2751

2852
def test_part_two():
29-
assert part_two(test_input) == 'x'
53+
assert part_two(test_input_2) == 281

0 commit comments

Comments
 (0)