Day6 Rust
This commit is contained in:
40
day6/rust/src/main.rs
Normal file
40
day6/rust/src/main.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
fn main() {
|
||||
let stdin = std::io::stdin();
|
||||
|
||||
let mut first = true;
|
||||
let mut times: Vec<usize> = Vec::new();
|
||||
let mut records: Vec<usize> = Vec::new();
|
||||
|
||||
for line in stdin.lines() {
|
||||
let line = line.unwrap();
|
||||
|
||||
let (_, numbers) = line.split_once(":").unwrap();
|
||||
|
||||
if first {
|
||||
times = numbers
|
||||
.split(" ")
|
||||
.map(|e| e.trim())
|
||||
.filter(|e| e.len() > 0)
|
||||
.map(|e| e.parse().unwrap())
|
||||
.collect();
|
||||
first = false;
|
||||
} else {
|
||||
records = numbers
|
||||
.split(" ")
|
||||
.map(|e| e.trim())
|
||||
.filter(|e| e.len() > 0)
|
||||
.map(|e| e.parse().unwrap())
|
||||
.collect();
|
||||
}
|
||||
}
|
||||
|
||||
let mut result = 1.0;
|
||||
|
||||
for (i, time) in times.iter().enumerate() {
|
||||
let min_d = ((*time as f64 - ((time * time - 4 * (records[i]+1)) as f64).sqrt()) / 2.0).ceil();
|
||||
let max_d = ((*time as f64 + ((time * time - 4 * (records[i]+1)) as f64).sqrt()) / 2.0).floor();
|
||||
result *= max_d - min_d + 1.0;
|
||||
}
|
||||
|
||||
println!("{}", result as usize);
|
||||
}
|
||||
Reference in New Issue
Block a user