Skip to content

Commit a7381b0

Browse files
committed
chore: don't compute sqrt for no good reason
1 parent 4125daf commit a7381b0

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

aoclp/src/positioning/pt_3d.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,13 @@ where
184184
(a.x - b.x).abs() + (a.y - b.y).abs() + (a.z - b.z).abs()
185185
}
186186

187-
/// Returns the [Euclidian distance] between two points in 3D space.
187+
/// Returns the square of the [Euclidian distance] between two points in 3D space.
188188
///
189189
/// [Euclidian distance]: https://en.wikipedia.org/wiki/Euclidean_distance
190-
pub fn euclidian<T>(a: Pt3d<T>, b: Pt3d<T>) -> f64
190+
pub fn euclidian_squared<T>(a: Pt3d<T>, b: Pt3d<T>) -> T
191191
where
192-
T: NumCast,
192+
T: Signed + Clone,
193193
{
194-
let (a, b) = (a.cast::<f64>(), b.cast::<f64>());
195-
((a.x - b.x).abs().powi(2) + (a.y - b.y).abs().powi(2) + (a.z - b.z).abs().powi(2)).sqrt()
194+
let (x, y, z) = ((a.x - b.x).abs(), (a.y - b.y).abs(), (a.z - b.z).abs());
195+
(x.clone() * x) + (y.clone() * y) + (z.clone() * z)
196196
}

aoclp_solutions/src/y2025/day_08.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22

33
use aoclp::num::Zero;
4-
use aoclp::positioning::pt_3d::{euclidian, Pt3d};
4+
use aoclp::positioning::pt_3d::{euclidian_squared, Pt3d};
55
use aoclp::solvers_impl::input::safe_get_input_as_many;
66
use itertools::Itertools;
77

@@ -29,8 +29,8 @@ fn circuits(all: bool) -> (HashMap<Pt3d, usize>, HashMap<usize, usize>, (Pt3d, P
2929
boxes
3030
.into_iter()
3131
.array_combinations()
32-
.map(|[a, b]| (a, b, euclidian(a, b)))
33-
.sorted_unstable_by(|(_, _, a), (_, _, b)| a.partial_cmp(b).unwrap())
32+
.map(|[a, b]| (a, b, euclidian_squared(a, b)))
33+
.sorted_unstable_by_key(|(_, _, d)| *d)
3434
.take(if all { 1_000_000 } else { 1_000 })
3535
.for_each(|(a, b, _)| match (circuits.get(&a).copied(), circuits.get(&b).copied()) {
3636
(Some(a_id), Some(b_id)) if a_id == b_id => (),

0 commit comments

Comments
 (0)