Add src/06part2.rs

This commit is contained in:
Cassandra de la Cruz-Munoz 2023-12-06 13:49:00 +01:00
parent 9292a54468
commit d5d866ca3a

27
src/06part2.rs Normal file
View File

@ -0,0 +1,27 @@
use std::fs;
fn main() {
let file_path = "input/06input.txt";
let contents = fs::read_to_string(file_path).expect("should read file");
let mut lines = contents.lines();
let time_str = lines.next().unwrap().strip_prefix("Time: ").unwrap();
let times = time_str.split_whitespace().fold(String::new(), |acc, x| acc + x).parse::<u128>().ok().unwrap();
let distance_str = lines.next().unwrap().strip_prefix("Distance: ").unwrap();
let distances = distance_str.split_whitespace().fold(String::new(), |acc, x| acc + x).parse::<u128>().ok().unwrap();
let mut total = 1;
let mut j = 0;
let mut k = times-1;
loop {
if j*(times-j) <= distances {
j += 1;
}
if k*(times-k) <= distances {
k -= 1;
}
if j*(times-j) > distances && k*(times-k) > distances {
total *= k - j + 1;
break;
}
}
println!("{}", total);
}