From 5c9d3942c6ec2f40c653e928b23f15a2a65c94f1 Mon Sep 17 00:00:00 2001 From: alyx Date: Wed, 6 Dec 2023 00:46:30 -0500 Subject: 2023.6 --- 2023/rs/Cargo.lock | 4 ++++ 2023/rs/code/six/Cargo.toml | 8 ++++++++ 2023/rs/code/six/src/input.txt | 2 ++ 2023/rs/code/six/src/main.rs | 28 ++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+) create mode 100644 2023/rs/code/six/Cargo.toml create mode 100644 2023/rs/code/six/src/input.txt create mode 100644 2023/rs/code/six/src/main.rs (limited to '2023') diff --git a/2023/rs/Cargo.lock b/2023/rs/Cargo.lock index e7c0d6f..0b77180 100644 --- a/2023/rs/Cargo.lock +++ b/2023/rs/Cargo.lock @@ -39,6 +39,10 @@ dependencies = [ name = "one" version = "0.1.0" +[[package]] +name = "six" +version = "0.1.0" + [[package]] name = "three" version = "0.1.0" 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::(); + 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}"); +} -- cgit v1.2.3-70-g09d2