Skip to content

Commit 8027a93

Browse files
feat: add 2023 day 1
1 parent 63534b7 commit 8027a93

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from adventofcode.util.exceptions import SolutionNotFoundError
2+
from adventofcode.registry.decorators import register_solution
3+
from adventofcode.util.input_helpers import get_input_for_day
4+
5+
DIGITS = {
6+
"one": "1",
7+
"two": "2",
8+
"three": "3",
9+
"four": "4",
10+
"five": "5",
11+
"six": "6",
12+
"seven": "7",
13+
"eight": "8",
14+
"nine": "9",
15+
}
16+
17+
18+
def get_calibration_value(line: str) -> int:
19+
left: str | None = None
20+
right: str | None = None
21+
22+
for char in line:
23+
if char.isdigit():
24+
right = char
25+
if left is None:
26+
left = char
27+
28+
return int(left + right)
29+
30+
31+
def get_calibration_values(lines: list[str]) -> int:
32+
total: int = 0
33+
34+
for line in lines:
35+
total += get_calibration_value(line)
36+
37+
return total
38+
39+
40+
@register_solution(2023, 1, 1)
41+
def part_one(input_data: list[str]):
42+
answer = get_calibration_values(input_data)
43+
44+
if not answer:
45+
raise SolutionNotFoundError(2023, 1, 1)
46+
47+
return answer
48+
49+
50+
@register_solution(2023, 1, 2)
51+
def part_two(input_data: list[str]):
52+
answer = ...
53+
54+
if not answer:
55+
raise SolutionNotFoundError(2023, 1, 2)
56+
57+
return answer
58+
59+
60+
if __name__ == '__main__':
61+
data = get_input_for_day(2023, 1)
62+
part_one(data)
63+
part_two(data)

tests/adventofcode/year_2023/__init__.py

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
3+
from adventofcode.year_2023.day_01_2023 import (part_two, part_one, get_calibration_value,
4+
)
5+
6+
test_input = [
7+
"1abc2",
8+
"pqr3stu8vwx",
9+
"a1b2c3d4e5f",
10+
"treb7uchet",
11+
]
12+
13+
14+
@pytest.mark.parametrize(["line", "expected"], [
15+
("1abc2", 12),
16+
("pqr3stu8vwx", 38),
17+
("a1b2c3d4e5f", 15),
18+
("treb7uchet", 77),
19+
])
20+
def test_get_calibration_value(line, expected):
21+
assert get_calibration_value(line) == expected
22+
23+
24+
def test_part_one():
25+
assert part_one(test_input) == 142
26+
27+
28+
def test_part_two():
29+
assert part_two(test_input) == 'x'

0 commit comments

Comments
 (0)