Skip to content

Commit 7011c9d

Browse files
committed
save state
1 parent 533c79c commit 7011c9d

File tree

2 files changed

+61
-34
lines changed

2 files changed

+61
-34
lines changed

src/advent_of_code/year_2025/day_01.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
DIAL_STARTING_POSITION = 50
44
DIAL_START = 0
55
DIAL_END = 99
6+
DIAL_SIZE = DIAL_END - DIAL_START + 1
67

78
def parse_instruction(instruction):
89
turn_direction = instruction[0]
@@ -17,25 +18,48 @@ def parse_instruction(instruction):
1718
return (turn_direction, distance)
1819

1920
def turn_dial(current_position, turn_direction, distance):
20-
return (current_position + turn_direction * distance) % (DIAL_END + 1)
21+
return (current_position + turn_direction * distance) % DIAL_SIZE
2122

22-
def position_counter(current_position, target_position=0, ):
23+
def target_position_counter(current_position, target_position=0, ):
2324
if current_position == target_position:
2425
return 1
2526
return 0
2627

28+
def points_at_zero_counter(current_position, turn_direction, distance):
29+
full_loops = distance // DIAL_SIZE
30+
31+
partial_loops = 0
32+
33+
# Check if we passed zero in the remaining distance after full loops
34+
new_position = turn_dial(current_position, turn_direction, distance)
35+
if new_position > current_position and turn_direction == -1 and current_position != 0 and new_position != 0:
36+
partial_loops += 1
37+
elif new_position < current_position and turn_direction == 1 and current_position != 0 and new_position != 0:
38+
partial_loops += 1
39+
40+
# check if we landed on zero
41+
if new_position == 0:
42+
partial_loops += 1
43+
44+
return full_loops + partial_loops
45+
2746
def solve(parsed_input):
2847

2948
current_position = DIAL_STARTING_POSITION
3049
target_position_count = 0
50+
points_at_zero_count = 0
3151

3252
for instruction in parsed_input:
3353
turn_direction, distance = parse_instruction(instruction)
34-
current_position = turn_dial(current_position, turn_direction, distance)
35-
target_position_count += position_counter(current_position)
54+
new_position = turn_dial(current_position, turn_direction, distance)
55+
56+
target_position_count += target_position_counter(new_position)
57+
points_at_zero_count += points_at_zero_counter(current_position, turn_direction, distance)
58+
# update start position
59+
current_position = new_position
3660

3761
part_1_solution = target_position_count
38-
part_2_solution = None
62+
part_2_solution = points_at_zero_count
3963
return (part_1_solution, part_2_solution)
4064

4165

tests/year_2025/test_day_01.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
from advent_of_code.year_2025.day_01 import (
33
solve,
44
turn_dial,
5-
position_counter,
5+
target_position_counter,
6+
points_at_zero_counter,
67
)
78

89

@@ -32,16 +33,16 @@ def test_solver(day_01_test_input, day_01_expected_output):
3233
assert result == day_01_expected_output
3334

3435
@pytest.fixture(params=[
35-
(50 , -1 , 68 , 82 , 0 , 1 ),
36-
(82 , -1 , 30 , 52 , 0 , 0 ),
37-
(52 , 1 , 48 , 0 , 1 , 1 ),
38-
(0 , -1 , 5 , 95 , 0 , 0 ),
39-
(95 , 1 , 60 , 55 , 0 , 1 ),
40-
(55 , -1 , 55 , 0 , 1 , 1 ),
41-
(0 , -1 , 1 , 99 , 0 , 0 ),
42-
(99 , -1 , 99 , 0 , 1 , 1 ),
43-
(0 , 1 , 14 , 14 , 0 , 0 ),
44-
(14 , -1 , 82 , 32 , 0 , 1 ),
36+
(50 , -1 , 68 , 82 , 0 , 1 ), # case 0
37+
(82 , -1 , 30 , 52 , 0 , 0 ), # case 1
38+
(52 , 1 , 48 , 0 , 1 , 1 ), # case 2
39+
(0 , -1 , 5 , 95 , 0 , 0 ), # case 3
40+
(95 , 1 , 60 , 55 , 0 , 1 ), # case 4
41+
(55 , -1 , 55 , 0 , 1 , 1 ), # case 5
42+
(0 , -1 , 1 , 99 , 0 , 0 ), # case 6
43+
(99 , -1 , 99 , 0 , 1 , 1 ), # case 7
44+
(0 , 1 , 14 , 14 , 0 , 0 ), # case 8
45+
(14 , -1 , 82 , 32 , 0 , 1 ), # case 9
4546
],
4647
)
4748
def turn_case(request):
@@ -57,25 +58,27 @@ def test_turn_dial(turn_case):
5758
assert new_position == expected_new_position
5859

5960

60-
def test_position_counter(turn_case):
61+
def test_target_position_counter(turn_case):
6162
_, _, _, expected_new_position, expected_count, _ = turn_case
62-
actual_count = position_counter(expected_new_position)
63+
actual_count = target_position_counter(expected_new_position)
6364
assert actual_count == expected_count
6465

66+
def test_points_at_zero_counter(turn_case):
67+
current_position, turn_direction, distance, _, _, expected_count = turn_case
68+
actual_count = points_at_zero_counter(current_position, turn_direction, distance)
69+
assert actual_count == expected_count
6570

66-
# @pytest.mark.parametrize(
67-
# "start_position, turn_direction, distance, expected_count",
68-
# [
69-
# (50, 1, 1000, 10),
70-
# # (50, 1, 49, 0),
71-
# # (50, -1, 49, 0),
72-
# # (50, -1, 50, 1),
73-
# # (50, -1, 51, 1),
74-
# # (99, 1, 1, 1),
75-
# # (99, -1, 1, 0),
76-
# # (0, 1, 100, 1),
77-
# ]
78-
# )
79-
# def test_passing_zero_counter(start_position, turn_direction, distance, expected_count):
80-
# count = passing_zero_counter(start_position, turn_direction, distance)
81-
# assert count == expected_count
71+
@pytest.mark.parametrize(
72+
"start_position, turn_direction, distance, expected_count",
73+
[
74+
(50, 1, 1000, 10),
75+
(50, 1, 1050, 11),
76+
(0, 1, 100, 1),
77+
(0, 1, 1, 0),
78+
(50 , -1, 68, 1),
79+
(50 , 1, 68, 1),
80+
]
81+
)
82+
def test_points_at_zero_counter_extra_test_cases(start_position, turn_direction, distance, expected_count):
83+
count = points_at_zero_counter(start_position, turn_direction, distance)
84+
assert count == expected_count

0 commit comments

Comments
 (0)