Skip to content

Commit e2d92a9

Browse files
committed
d3 part 2
1 parent 9014532 commit e2d92a9

File tree

15 files changed

+96
-48
lines changed

15 files changed

+96
-48
lines changed

src/advent_of_code/year_2024/day_04.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import numpy as np
2-
32
from advent_of_code.utils.input_handling import read_input
43

54
STRING_TO_FIND = "XMAS"

src/advent_of_code/year_2025/day_02.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from advent_of_code.utils.input_handling import read_input
22

3-
VALID_PARTS = (1,2)
3+
VALID_PARTS = (1, 2)
4+
45

56
def parse_input(raw):
67
raw_split = raw[0].split(",")
@@ -25,17 +26,23 @@ def check_id_valid(id):
2526
else:
2627
return True
2728

29+
2830
def check_id_valid_v2(id):
2931
id_len = len(str(id))
3032
half_len = id_len // 2
31-
for sub_str_len in range(1,half_len+1):
33+
for sub_str_len in range(1, half_len + 1):
3234
sub_str = id[:sub_str_len]
3335
str_to_search = id[sub_str_len:]
34-
if (len(str_to_search) % sub_str_len == 0): # substr divisible by search str length
35-
if (sub_str * (id_len // sub_str_len) == id): # substr matches N * search string
36+
if (
37+
len(str_to_search) % sub_str_len == 0
38+
): # substr divisible by search str length
39+
if (
40+
sub_str * (id_len // sub_str_len) == id
41+
): # substr matches N * search string
3642
return False
3743
return True
3844

45+
3946
def get_invalid_ids_in_range(range_pair, part):
4047
full_range = range(range_pair[0], range_pair[1] + 1)
4148

@@ -45,7 +52,7 @@ def get_invalid_ids_in_range(range_pair, part):
4552
id_checker_function = check_id_valid_v2
4653
else:
4754
raise ValueError("Specify a valid part number")
48-
55+
4956
invalid_ids = []
5057
for x in full_range:
5158
if not id_checker_function(str(x)):
@@ -74,10 +81,12 @@ def solve_part(parsed_input, part):
7481
invalid_ids = find_all_invalid_ids(parsed_input, part)
7582
return sum_invalid_ids(invalid_ids)
7683

84+
7785
def solve_part_2(parsed_input):
7886
invalid_ids = find_all_invalid_ids(parsed_input)
7987
return sum_invalid_ids(invalid_ids)
8088

89+
8190
def solve(parsed_input):
8291
part_1_solution = solve_part(parsed_input, part=1)
8392
part_2_solution = solve_part(parsed_input, part=2)

src/advent_of_code/year_2025/day_03.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from advent_of_code.utils.input_handling import read_input
22

3+
34
def parse_battery_bank_to_ints_list(battery_bank_str):
45
return [int(x) for x in battery_bank_str]
56

@@ -8,18 +9,34 @@ def combine_joltages(list_of_joltages):
89
return int("".join([str(x) for x in list_of_joltages]))
910

1011

12+
def find_max_and_leftmost_index(input_list):
13+
max_value = max(input_list)
14+
leftmost_index = input_list.index(max_value)
15+
return max_value, leftmost_index
16+
17+
1118
def calculate_largest_joltage(battery_bank_as_ints, n_batteries):
12-
largest_joltage_1 = 0
13-
14-
for battery_index, battery_value in enumerate(battery_bank_as_ints):
15-
if (battery_value > largest_joltage_1) and (battery_index < len(battery_bank_as_ints) - 1):
16-
largest_joltage_1 = battery_value
17-
largest_joltage_2 = 0
18-
for second_battery_value in battery_bank_as_ints[battery_index+1:]:
19-
if second_battery_value > largest_joltage_2:
20-
largest_joltage_2 = second_battery_value
21-
22-
return combine_joltages([largest_joltage_1, largest_joltage_2])
19+
20+
batteries_remaining = n_batteries
21+
bank_len = len(battery_bank_as_ints)
22+
list_of_joltages = []
23+
start_ix = 0
24+
25+
while batteries_remaining > 0:
26+
end_ix = bank_len - batteries_remaining + 1
27+
window_to_check = battery_bank_as_ints[start_ix:end_ix]
28+
29+
# find largest value in window and the left-most index for dupes
30+
max_value, leftmost_index_window = find_max_and_leftmost_index(window_to_check)
31+
leftmost_index = leftmost_index_window + start_ix
32+
list_of_joltages.append(max_value)
33+
34+
# reassign start_ix and update remaining batteries
35+
start_ix = leftmost_index + 1
36+
batteries_remaining -= 1
37+
38+
print(f"list_of_joltages={list_of_joltages}")
39+
return combine_joltages(list_of_joltages)
2340

2441

2542
def solve_part(input, n_batteries):
@@ -32,6 +49,7 @@ def solve_part(input, n_batteries):
3249

3350
return sum(joltage_list)
3451

52+
3553
def solve(input):
3654
part_1_solution = solve_part(input, n_batteries=2)
3755
part_2_solution = solve_part(input, n_batteries=12)

tests/year_2023/test_day_01.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
32
from advent_of_code.year_2023.day_01 import (
43
convert_first_last_patterns_to_calibration_value,
54
convert_str_to_numerical,

tests/year_2023/test_day_02.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
32
from advent_of_code.year_2023.day_02 import (
43
check_game_is_possible,
54
find_max_shown_for_colour,

tests/year_2023/test_day_03.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
32
from advent_of_code.year_2023.day_03 import (
43
Part,
54
Symbol,

tests/year_2023/test_day_04.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
32
from advent_of_code.year_2023.day_04 import (
43
compute_copies,
54
compute_total_score,

tests/year_2023/test_day_06.py

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

33
import pytest
4-
54
from advent_of_code.year_2023.day_06 import (
65
Race,
76
Races,

tests/year_2024/test_day_01.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
32
from advent_of_code.year_2024.day_01 import (
43
solve,
54
)

tests/year_2024/test_day_02.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
32
from advent_of_code.year_2024.day_02 import (
43
parse_input,
54
solve,

0 commit comments

Comments
 (0)