diff options
Diffstat (limited to '2023/rs/code/six')
-rw-r--r-- | 2023/rs/code/six/Cargo.toml | 8 | ||||
-rw-r--r-- | 2023/rs/code/six/src/input.txt | 2 | ||||
-rw-r--r-- | 2023/rs/code/six/src/main.rs | 28 |
3 files changed, 38 insertions, 0 deletions
diff --git a/2023/rs/code/six/Cargo.toml b/2023/rs/code/six/Cargo.toml new file mode 100644 index 0000000..8413a35 --- /dev/null +++ b/2023/rs/code/six/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "six" +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/2023/rs/code/six/src/input.txt b/2023/rs/code/six/src/input.txt new file mode 100644 index 0000000..27b64c4 --- /dev/null +++ b/2023/rs/code/six/src/input.txt @@ -0,0 +1,2 @@ +Time: 58 81 96 76 +Distance: 434 1041 2219 1218 diff --git a/2023/rs/code/six/src/main.rs b/2023/rs/code/six/src/main.rs new file mode 100644 index 0000000..3504380 --- /dev/null +++ b/2023/rs/code/six/src/main.rs @@ -0,0 +1,28 @@ +static INPUT: &str = include_str!("input.txt"); + +#[inline(always)] +fn exceeding_count(time: u64, dist: u64) -> u64 { + let fac = (2..time).find(|a| a * (time - a) > dist).unwrap(); + time - fac * 2 + 1 +} + +fn main() { + let (times, distances) = { + let mut lines = INPUT.split('\n').filter(|s| !s.is_empty()); + let times = lines.next().and_then(|l| l.split(":").nth(1)).unwrap(); + let distances = lines.next().and_then(|l| l.split(":").nth(1)).unwrap(); + (times, distances) + }; + + let time_dist = { + let times = times.split_ascii_whitespace().map(|a| a.parse().unwrap()); + let distances = distances.split_ascii_whitespace().map(|a| a.parse().unwrap()); + times.zip(distances) + }; + let ways = time_dist.map(|(t,d)| exceeding_count(t, d)).product::<u64>(); + println!("Number of ways to beat past 'winners'(losers): {ways}"); + + let (bigtime, bigdistance) = (times.replace(' ', "").parse().unwrap(), distances.replace(' ', "").parse().unwrap()); + let ways = exceeding_count(bigtime, bigdistance); + println!("Number of ways to beat past (really long race) 'winners'(losers): {ways}"); +} |