mirror of
https://github.com/Baipyrus/AoC-23.git
synced 2024-12-26 03:51:45 +00:00
calculate manhattan distance of pairs and sum
This commit is contained in:
parent
d0f0a31d14
commit
db747ef3fe
@ -11,6 +11,22 @@ pub fn part_one() {
|
||||
|
||||
let mut galaxies = extract(input);
|
||||
expand(&mut galaxies);
|
||||
let distances = sum_dist(galaxies);
|
||||
|
||||
println!("The sum of all distances is: '{distances}'.");
|
||||
}
|
||||
|
||||
fn sum_dist(galaxies: Vec<Galaxy>) -> usize {
|
||||
let mut distances = 0;
|
||||
|
||||
for (i, a) in galaxies.iter().enumerate() {
|
||||
for b in galaxies.iter().skip(i) {
|
||||
if b.compare(a) { continue; }
|
||||
distances += a.distance(b);
|
||||
}
|
||||
}
|
||||
|
||||
distances
|
||||
}
|
||||
|
||||
fn extract(symbols: Vec<Vec<String>>) -> Vec<Galaxy> {
|
||||
@ -36,6 +52,17 @@ struct Galaxy {
|
||||
y: usize,
|
||||
}
|
||||
|
||||
impl Galaxy {
|
||||
fn distance(&self, other: &Galaxy) -> usize {
|
||||
let dx = (other.x as isize - self.x as isize).abs();
|
||||
let dy = (other.y as isize - self.y as isize).abs();
|
||||
(dx + dy) as usize
|
||||
}
|
||||
|
||||
fn compare(&self, other: &Galaxy) -> bool {
|
||||
self.x == other.x && self.y == other.y
|
||||
}
|
||||
}
|
||||
|
||||
fn setup() -> Vec<Vec<String>> {
|
||||
let name = "day11";
|
||||
|
Loading…
Reference in New Issue
Block a user