diff --git a/day6/Cargo.lock b/day6/Cargo.lock new file mode 100644 index 0000000..a16c0bf --- /dev/null +++ b/day6/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day6" +version = "0.1.0" diff --git a/day6/Cargo.toml b/day6/Cargo.toml new file mode 100644 index 0000000..89d04ae --- /dev/null +++ b/day6/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day6" +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/input.txt b/day6/input.txt new file mode 100644 index 0000000..09a1c8d --- /dev/null +++ b/day6/input.txt @@ -0,0 +1,2 @@ +Time: 63 78 94 68 +Distance: 411 1274 2047 1035 \ No newline at end of file diff --git a/day6/src/main.rs b/day6/src/main.rs new file mode 100644 index 0000000..4f7476f --- /dev/null +++ b/day6/src/main.rs @@ -0,0 +1,30 @@ +use std::fs; + +fn main() { + let input = fs::read_to_string("input.txt").unwrap(); + let input: Vec<_> = input.split('\n') // Seperate the Time and Distance lines + .map(|line| { + &line[line.find(':').unwrap() + 1..] // Drop "Time:" and "Distance:" + }).map(|line| { + line.split_whitespace() // Split the numbers into their own elements. + .map(|num| num.parse::().expect("Couldn't parse number")) // Parse numbers into i32 + .collect::>() + }).collect(); // collect into Vec + + let mut valid_total = 1; + + for round in 0..input.first().unwrap().len() { + let time = input[0][round]; + let dist = input[1][round]; + let mut valid = 0; + + for remaining_time in 0..time { + if (time - remaining_time) * remaining_time > dist { + valid += 1; + } + } + valid_total *= valid; + } + + println!("{}", valid_total); +}