Add src/06part1.rs

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

29
src/06part1.rs Normal file
View File

@ -0,0 +1,29 @@
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: Vec<u128> = time_str.split_whitespace().map(|x| x.parse::<u128>().ok().unwrap()).collect();
let distance_str = lines.next().unwrap().strip_prefix("Distance: ").unwrap();
let distances: Vec<u128> = distance_str.split_whitespace().map(|x| x.parse::<u128>().ok().unwrap()).collect();
let mut total = 1;
for i in 0..times.len() {
let mut j = 0;
let mut k = times[i]-1;
loop {
if j*(times[i]-j) <= distances[i] {
j += 1;
}
if k*(times[i]-k) <= distances[i] {
k -= 1;
}
if j*(times[i]-j) > distances[i] && k*(times[i]-k) > distances[i] {
total *= k - j + 1;
break;
}
}
}
println!("{}", total);
}