Compare commits
3 Commits
00000050d9
...
00000080ac
| Author | SHA1 | Date | |
|---|---|---|---|
|
00000080ac
|
|||
|
0000007071
|
|||
|
00000060e4
|
2
.git-blame-ignore-revs
Normal file
2
.git-blame-ignore-revs
Normal file
@@ -0,0 +1,2 @@
|
||||
# lint & format
|
||||
0000007071a01bec5b648d00bbc2055eef6f59e0
|
||||
@@ -62,7 +62,8 @@ def run_day(year: int, day: int, input_data: str, path_base: pathlib.Path) -> No
|
||||
spec.loader.exec_module(solution_module)
|
||||
except (ModuleNotFoundError, exceptions.AocError) as e:
|
||||
raise exceptions.AocError(
|
||||
f"solution module for {year}/{day:02} not found: run 'python main.py create --year {year} {day}'"
|
||||
f"solution module for {year}/{day:02} not found: "
|
||||
f"run 'python main.py create --year {year} {day}'"
|
||||
) from e
|
||||
|
||||
p_1 = getattr(solution_module, "part_1", None)
|
||||
|
||||
4
data/example/2025/03.txt
Normal file
4
data/example/2025/03.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
987654321111111
|
||||
811111111111119
|
||||
234234234234278
|
||||
818181911112111
|
||||
@@ -10,8 +10,6 @@ select = [
|
||||
'UP', # pyupgrade
|
||||
'B', # flake8-bugbear
|
||||
'C', # flake8-comprehensions
|
||||
'DTZ', # flake8-datetimez
|
||||
'DJ', # flake8-django
|
||||
'RUF', # ruff
|
||||
'N', # pep8-naming
|
||||
]
|
||||
|
||||
64
src/solution/year_2025/day_03.py
Normal file
64
src/solution/year_2025/day_03.py
Normal file
@@ -0,0 +1,64 @@
|
||||
from typing import Any
|
||||
|
||||
|
||||
def find_max_subnumber(numbers: list[int], length: int) -> int:
|
||||
numbers = list(reversed(numbers))
|
||||
max_search_index = len(numbers)
|
||||
n = 0
|
||||
|
||||
if length > len(numbers):
|
||||
raise ValueError("length greater than numbers length")
|
||||
|
||||
for start_idx in range(length - 1, -1, -1):
|
||||
# local number and loacal max search index
|
||||
ln, lmsi = 0, 0
|
||||
# search from start_idx up to max_search_index
|
||||
for idx in range(start_idx, max_search_index):
|
||||
m = numbers[idx]
|
||||
if idx >= max_search_index:
|
||||
max_search_index = lmsi
|
||||
break
|
||||
# find leftmost highst number and update found digit and local max index
|
||||
if m >= ln:
|
||||
ln = m
|
||||
lmsi = idx
|
||||
|
||||
max_search_index = lmsi
|
||||
n += ln
|
||||
n *= 10
|
||||
|
||||
return n // 10
|
||||
|
||||
|
||||
def part_1(input_data: str) -> Any:
|
||||
s = 0
|
||||
for line in input_data.splitlines():
|
||||
parsed = list(map(int, line))
|
||||
s += find_max_subnumber(parsed, 2)
|
||||
return s
|
||||
|
||||
|
||||
def part_2(input_data: str) -> Any:
|
||||
s = 0
|
||||
for line in input_data.splitlines():
|
||||
parsed = list(map(int, line))
|
||||
s += find_max_subnumber(parsed, 12)
|
||||
return s
|
||||
|
||||
|
||||
def test_find_max_subnumber():
|
||||
assert find_max_subnumber([1, 2, 3, 4, 9], 2) == 49
|
||||
assert find_max_subnumber([1, 2, 3, 4, 5], 2) == 45
|
||||
assert find_max_subnumber([5, 4, 3, 2, 1], 2) == 54
|
||||
assert find_max_subnumber([1, 3, 5, 2, 4], 2) == 54
|
||||
assert find_max_subnumber([8, 1, 8, 2, 7, 3], 3) == 887
|
||||
assert find_max_subnumber([1, 9, 2, 8, 3, 7], 3) == 987
|
||||
assert find_max_subnumber([1, 2, 3, 4, 5], 3) == 345
|
||||
|
||||
|
||||
def test_part_1(example_data):
|
||||
assert part_1(example_data) == 357
|
||||
|
||||
|
||||
def test_part_2(example_data):
|
||||
assert part_2(example_data) == 3121910778619
|
||||
Reference in New Issue
Block a user