diff options
author | alyx <alyx@aleteoryx.me> | 2023-12-06 00:46:30 -0500 |
---|---|---|
committer | alyx <alyx@aleteoryx.me> | 2023-12-06 00:46:30 -0500 |
commit | 5c9d3942c6ec2f40c653e928b23f15a2a65c94f1 (patch) | |
tree | 690e5d52013cd662cab655113fa7f3a14f76e397 /2023/rs/code/six/src | |
parent | 2ee297eb8768676ca9e3162bf0e1b71066afb3ec (diff) | |
download | adventofcode-5c9d3942c6ec2f40c653e928b23f15a2a65c94f1.tar.gz adventofcode-5c9d3942c6ec2f40c653e928b23f15a2a65c94f1.tar.bz2 adventofcode-5c9d3942c6ec2f40c653e928b23f15a2a65c94f1.zip |
2023.6
Diffstat (limited to '2023/rs/code/six/src')
-rw-r--r-- | 2023/rs/code/six/src/input.txt | 2 | ||||
-rw-r--r-- | 2023/rs/code/six/src/main.rs | 28 |
2 files changed, 30 insertions, 0 deletions
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}"); +} |