diff --git a/day6/rust/.gitignore b/day6/rust/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/day6/rust/.gitignore @@ -0,0 +1 @@ +target diff --git a/day6/rust/Cargo.lock b/day6/rust/Cargo.lock new file mode 100644 index 0000000..b21cc6a --- /dev/null +++ b/day6/rust/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "rust" +version = "0.1.0" diff --git a/day6/rust/Cargo.toml b/day6/rust/Cargo.toml new file mode 100644 index 0000000..1ec6963 --- /dev/null +++ b/day6/rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rust" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day6/rust/src/main.rs b/day6/rust/src/main.rs new file mode 100644 index 0000000..4ef4f7f --- /dev/null +++ b/day6/rust/src/main.rs @@ -0,0 +1,40 @@ +fn main() { + let stdin = std::io::stdin(); + + let mut first = true; + let mut times: Vec = Vec::new(); + let mut records: Vec = 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); +}