part 2 complete
This commit is contained in:
@@ -1,6 +1,35 @@
|
||||
import os
|
||||
|
||||
with open(r'advent_of_code\2023\day_6\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
input = file.readlines()
|
||||
with open(r'advent_of_code\2023\day_6\test_input.txt', 'r') as file:
|
||||
test_input = file.readlines()
|
||||
|
||||
print(input)
|
||||
#input = test_input
|
||||
|
||||
times_string = input[0].replace('Time:', '').replace(' ', '')
|
||||
time = int(times_string)
|
||||
|
||||
distances_string = input[1].replace('Distance:', '').replace(' ', '')
|
||||
distance = int(distances_string)
|
||||
|
||||
print(time, distance)
|
||||
|
||||
def calculate_distances(race_time):
|
||||
max_distances = []
|
||||
for button_hold_time in range(race_time + 1):
|
||||
travel_time = race_time - button_hold_time
|
||||
distance_travelled = button_hold_time * travel_time
|
||||
max_distances.append(distance_travelled)
|
||||
return max_distances
|
||||
|
||||
count_valid_distances = []
|
||||
|
||||
max_distances = calculate_distances(time)
|
||||
valid_distances = [max_distance for max_distance in max_distances if max_distance > distance]
|
||||
count_valid_distances.append(len(valid_distances))
|
||||
|
||||
#multiply the count of valid distances together
|
||||
print(count_valid_distances)
|
||||
result = 1
|
||||
for num in count_valid_distances:
|
||||
result *= num
|
||||
print(result)
|
||||
@@ -41,4 +41,18 @@ In the third race, you could hold the button for at least 11 milliseconds and no
|
||||
|
||||
To see how much margin of error you have, determine the number of ways you can beat the record in each race; in this example, if you multiply these values together, you get 288 (4 * 8 * 9).
|
||||
|
||||
Determine the number of ways you could beat the record in each race. What do you get if you multiply these numbers together?
|
||||
Determine the number of ways you could beat the record in each race. What do you get if you multiply these numbers together?
|
||||
|
||||
|
||||
--- Part Two ---
|
||||
As the race is about to start, you realize the piece of paper with race times and record distances you got earlier actually just has very bad kerning. There's really only one race - ignore the spaces between the numbers on each line.
|
||||
|
||||
So, the example from before:
|
||||
|
||||
Time: 7 15 30
|
||||
Distance: 9 40 200
|
||||
...now instead means this:
|
||||
|
||||
Time: 71530
|
||||
Distance: 940200
|
||||
Now, you have to figure out how many ways there are to win this single race. In this example, the race lasts for 71530 milliseconds and the record distance you need to beat is 940200 millimeters. You could hold the button anywhere from 14 to 71516 milliseconds and beat the record, a total of 71503 ways!
|
||||
Reference in New Issue
Block a user