From 30935708d1ec98975be3607605688a59948206a3 Mon Sep 17 00:00:00 2001 From: Jake Pullen Date: Wed, 6 Dec 2023 06:19:48 +0000 Subject: [PATCH] part 2 complete --- 2023/day_6/part 2 solution.py | 37 +++++++++++++++++++++++++++++++---- 2023/day_6/puzzle_text.md | 16 ++++++++++++++- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/2023/day_6/part 2 solution.py b/2023/day_6/part 2 solution.py index dd3f772..3c2dec7 100644 --- a/2023/day_6/part 2 solution.py +++ b/2023/day_6/part 2 solution.py @@ -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) \ No newline at end of file diff --git a/2023/day_6/puzzle_text.md b/2023/day_6/puzzle_text.md index aefbaa7..56ed478 100644 --- a/2023/day_6/puzzle_text.md +++ b/2023/day_6/puzzle_text.md @@ -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? \ No newline at end of file +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! \ No newline at end of file