From be3ee7ea2afd5ff76ae5aed33d970819d5bebfb2 Mon Sep 17 00:00:00 2001 From: Kevin Caffrey Date: Sun, 24 Dec 2023 16:16:17 -0500 Subject: [PATCH] day 24 part 1 use rayon for big speedups --- README.md | 4 +-- src/bin/24.rs | 71 +++++++++++++++++++++++++++------------------------ 2 files changed, 40 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 9bea209..8a91fbc 100644 --- a/README.md +++ b/README.md @@ -62,9 +62,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www. | [Day 21](./src/bin/21.rs) | `64.1µs` | `494.7µs` | | [Day 22](./src/bin/22.rs) | `78.3µs` | `194.8µs` | | [Day 23](./src/bin/23.rs) | `330.4µs` | `13.3ms` | -| [Day 24](./src/bin/24.rs) | `369.6µs` | `-` | +| [Day 24](./src/bin/24.rs) | `114.9µs` | `-` | -**Total: 30.61ms** +**Total: 30.35ms** --- diff --git a/src/bin/24.rs b/src/bin/24.rs index 4fe5291..837e767 100644 --- a/src/bin/24.rs +++ b/src/bin/24.rs @@ -1,3 +1,5 @@ +use rayon::iter::{IntoParallelIterator, ParallelIterator}; + advent_of_code::solution!(24); pub fn part_one(input: &str) -> Option { @@ -15,40 +17,43 @@ fn count_xy_intersections>( max_position: i64, ) -> u32 { let hailstones = hailstones.as_ref(); - let mut count = 0; - for i in 0..hailstones.len() { - for j in i + 1..hailstones.len() { - let mut a = hailstones[i]; - let mut b = hailstones[j]; - if a.velocity.x == 0 || b.velocity.y * a.velocity.x == a.velocity.y * b.velocity.x { - std::mem::swap(&mut a, &mut b); - } - if a.velocity.x == 0 || b.velocity.y * a.velocity.x == a.velocity.y * b.velocity.x { - // Can't intersect? - continue; - } - let t = (a.position.y * a.velocity.x + a.velocity.y * b.position.x - - a.velocity.y * a.position.x - - b.position.y * a.velocity.x) as f64 - / (b.velocity.y * a.velocity.x - a.velocity.y * b.velocity.x) as f64; - let s = ((b.position.x - a.position.x) as f64 + b.velocity.x as f64 * t) - / a.velocity.x as f64; - if t < 0.0 || s < 0.0 { - // Intersection in past. - continue; + (0..hailstones.len()) + .into_par_iter() + .map(|i| { + let mut count = 0; + for j in i + 1..hailstones.len() { + let mut a = hailstones[i]; + let mut b = hailstones[j]; + if a.velocity.x == 0 || b.velocity.y * a.velocity.x == a.velocity.y * b.velocity.x { + std::mem::swap(&mut a, &mut b); + } + if a.velocity.x == 0 || b.velocity.y * a.velocity.x == a.velocity.y * b.velocity.x { + // Can't intersect? + continue; + } + let t = (a.position.y * a.velocity.x + a.velocity.y * b.position.x + - a.velocity.y * a.position.x + - b.position.y * a.velocity.x) as f64 + / (b.velocity.y * a.velocity.x - a.velocity.y * b.velocity.x) as f64; + let s = ((b.position.x - a.position.x) as f64 + b.velocity.x as f64 * t) + / a.velocity.x as f64; + if t < 0.0 || s < 0.0 { + // Intersection in past. + continue; + } + let x = a.position.x as f64 + a.velocity.x as f64 * s; + let y = a.position.y as f64 + a.velocity.y as f64 * s; + if x >= min_position as f64 + && x <= max_position as f64 + && y >= min_position as f64 + && y <= max_position as f64 + { + count += 1; + } } - let x = a.position.x as f64 + a.velocity.x as f64 * s; - let y = a.position.y as f64 + a.velocity.y as f64 * s; - if x >= min_position as f64 - && x <= max_position as f64 - && y >= min_position as f64 - && y <= max_position as f64 - { - count += 1; - } - } - } - count + count + }) + .sum() } pub fn part_two(input: &str) -> Option {