feat: add solution 2025/03
This commit is contained in:
4
data/example/2025/03.txt
Normal file
4
data/example/2025/03.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
987654321111111
|
||||||
|
811111111111119
|
||||||
|
234234234234278
|
||||||
|
818181911112111
|
||||||
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