Add src/06part2.rs
This commit is contained in:
parent
9292a54468
commit
d5d866ca3a
27
src/06part2.rs
Normal file
27
src/06part2.rs
Normal 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);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user