Skip to content

Commit bc5633a

Browse files
authored
[py] 2023:06: Improve part2
1 parent 66cd61a commit bc5633a

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

2023/day06/part2.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,22 @@ def calculate_distance(windup: int, total: int):
2525
time: int = int("".join([str(t) for t in get_values("time")]))
2626
dist: int = int("".join([str(d) for d in get_values("distance")]))
2727

28-
winning_windups: int = 0
29-
for windup in tqdm(range(time)):
30-
reached_dist: int = calculate_distance(windup, time)
31-
if reached_dist > dist:
32-
winning_windups += 1
33-
print(f"SOLVE: {winning_windups}")
28+
29+
def is_winning(windup: int) -> bool:
30+
global time
31+
global dist
32+
return calculate_distance(windup, time) > dist
33+
34+
35+
lowest: int = 0
36+
while not is_winning(lowest):
37+
lowest += 1
38+
print(f"Lowest: {lowest}")
39+
40+
highest: int = time
41+
while not is_winning(highest):
42+
highest -= 1
43+
print(f"Highest: {highest}")
44+
45+
solution: int = highest - lowest + 1
46+
print(f"SOLVE: {solution}")

0 commit comments

Comments
 (0)