Skip to content

Commit

Permalink
day 24 part 1 use rayon for big speedups
Browse files Browse the repository at this point in the history
  • Loading branch information
kcaffrey committed Dec 24, 2023
1 parent b058151 commit be3ee7e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
<!--- benchmarking table --->

---
Expand Down
71 changes: 38 additions & 33 deletions src/bin/24.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use rayon::iter::{IntoParallelIterator, ParallelIterator};

advent_of_code::solution!(24);

pub fn part_one(input: &str) -> Option<u32> {
Expand All @@ -15,40 +17,43 @@ fn count_xy_intersections<T: AsRef<[Hailstone]>>(
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<u32> {
Expand Down

0 comments on commit be3ee7e

Please sign in to comment.