prettify and lint optimization
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -6,11 +6,25 @@ fn parse_input(file_path: &str) -> io::Result<(i64, i64)> {
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
let mut lines = reader.lines();
|
||||
let time_line = lines.next().ok_or(io::Error::new(io::ErrorKind::Other, "Missing time line"))??;
|
||||
let distance_line = lines.next().ok_or(io::Error::new(io::ErrorKind::Other, "Missing distance line"))??;
|
||||
let time_line = lines
|
||||
.next()
|
||||
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Missing time line"))??;
|
||||
let distance_line = lines
|
||||
.next()
|
||||
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Missing distance line"))??;
|
||||
|
||||
let time = time_line.chars().filter(|c| c.is_digit(10)).collect::<String>().parse::<i64>().unwrap_or(0);
|
||||
let distance = distance_line.chars().filter(|c| c.is_digit(10)).collect::<String>().parse::<i64>().unwrap_or(0);
|
||||
let time = time_line
|
||||
.chars()
|
||||
.filter(|c| c.is_ascii_digit())
|
||||
.collect::<String>()
|
||||
.parse::<i64>()
|
||||
.unwrap_or(0);
|
||||
let distance = distance_line
|
||||
.chars()
|
||||
.filter(|c| c.is_ascii_digit())
|
||||
.collect::<String>()
|
||||
.parse::<i64>()
|
||||
.unwrap_or(0);
|
||||
|
||||
Ok((time, distance))
|
||||
}
|
||||
@@ -30,7 +44,11 @@ fn calculate_winning_ways(time: i64, record: i64) -> i64 {
|
||||
fn run_test(file_path: &str, expected_result: i64) -> io::Result<()> {
|
||||
let (time, distance) = parse_input(file_path)?;
|
||||
let result = calculate_winning_ways(time, distance);
|
||||
assert_eq!(result, expected_result, "Test failed! Expected {}, got {}", expected_result, result);
|
||||
assert_eq!(
|
||||
result, expected_result,
|
||||
"Test failed! Expected {}, got {}",
|
||||
expected_result, result
|
||||
);
|
||||
println!("Test passed successfully.");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user