prettify and lint optimization

This commit is contained in:
2023-12-10 17:24:14 +01:00
parent 274c69d0c4
commit 4cd62ef794
32 changed files with 599 additions and 262 deletions

View File

@@ -1,12 +1,15 @@
def parse_input(file_path):
"""Parse the input file into race times and record distances."""
with open(file_path, 'r') as file:
with open(file_path, "r") as file:
lines = file.readlines()
times = [int(value) for value in lines[0].strip().split() if value.isdigit()]
distances = [int(value) for value in lines[1].strip().split() if value.isdigit()]
distances = [
int(value) for value in lines[1].strip().split() if value.isdigit()
]
print(f"Input parsed. Times: {times}, Distances: {distances}")
return times, distances
def calculate_winning_ways(time, record):
"""Calculate the number of ways to beat the record for a single race."""
print(f"Calculating winning ways for time: {time}, record: {record}")
@@ -16,13 +19,16 @@ def calculate_winning_ways(time, record):
speed = hold_time
remaining_time = time - hold_time
distance = speed * remaining_time
print(f"Hold Time: {hold_time}, Speed: {speed}, Remaining Time: {remaining_time}, Distance: {distance}")
print(
f"Hold Time: {hold_time}, Speed: {speed}, Remaining Time: {remaining_time}, Distance: {distance}"
)
# Count as winning way if distance is greater than the record
if distance > record:
winning_ways += 1
print(f"Winning ways for this race: {winning_ways}")
return winning_ways
def total_winning_combinations(times, distances):
"""Calculate the total number of winning combinations for all races."""
print("Calculating total winning combinations.")
@@ -36,6 +42,7 @@ def total_winning_combinations(times, distances):
print(f"Total winning combinations: {total_combinations}")
return total_combinations
def run_test(file_path):
"""Run the test with assertions to verify the functionality."""
print(f"Running test with file: {file_path}")
@@ -45,6 +52,7 @@ def run_test(file_path):
assert result == 288, f"Test failed! Expected 288, got {result}"
print("Test passed successfully.")
def main():
"""Main function to run the test and then process the input file."""
try:
@@ -64,5 +72,6 @@ def main():
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
main()

View File

@@ -3,13 +3,14 @@ def parse_input(file_path):
Parse the input file to extract the race time and record distance.
Assumes the file has two lines: first for time, second for distance.
"""
with open(file_path, 'r') as file:
with open(file_path, "r") as file:
lines = file.readlines()
time = int(''.join(filter(str.isdigit, lines[0].strip())))
distance = int(''.join(filter(str.isdigit, lines[1].strip())))
time = int("".join(filter(str.isdigit, lines[0].strip())))
distance = int("".join(filter(str.isdigit, lines[1].strip())))
print(f"Parsed input from {file_path} - Time: {time}, Distance: {distance}")
return time, distance
def calculate_winning_ways(time, record):
"""
Calculate the number of ways to beat the record for the race.
@@ -25,9 +26,12 @@ def calculate_winning_ways(time, record):
max_hold_time -= 1
winning_ways = max(0, max_hold_time - min_hold_time + 1)
print(f"Winning ways calculated: {winning_ways} (Min: {min_hold_time}, Max: {max_hold_time})")
print(
f"Winning ways calculated: {winning_ways} (Min: {min_hold_time}, Max: {max_hold_time})"
)
return winning_ways
def run_test(file_path, expected_result):
"""
Run a test with the given file and compare the result to the expected result.
@@ -36,9 +40,12 @@ def run_test(file_path, expected_result):
time, record = parse_input(file_path)
result = calculate_winning_ways(time, record)
print(f"Test result: {result}")
assert result == expected_result, f"Test failed! Expected {expected_result}, got {result}"
assert (
result == expected_result
), f"Test failed! Expected {expected_result}, got {result}"
print("Test passed successfully.")
def main():
"""
Main function to run the test and then process the actual input file.
@@ -61,5 +68,6 @@ def main():
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
main()