Skip to content

Commit

Permalink
fix: Make day11 solver more effecient by not being lazy and doing double
Browse files Browse the repository at this point in the history
the work and dividing by 2
  • Loading branch information
Traviis committed Dec 11, 2023
1 parent a544328 commit 8f6a5ea
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/day11.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;
use rayon::prelude::*;
use std::collections::{HashMap, HashSet};

type InputType = HashMap<(u64, u64), Thing>;
type OutputType = u64;
Expand Down Expand Up @@ -169,20 +168,30 @@ pub fn part1(input: &InputType) -> OutputType {

pub fn solver(input: &InputType, expansion_rate: u64) -> OutputType {
let input = expand_galaxy(input,expansion_rate);

let mut seen_combo = HashSet::new();

input.keys().map(|(x,y)| {

let mut dist = 0;
for (x2,y2) in input.keys() {
if x == x2 && y == y2 {
continue;
}
dist += manhatten_distance((*x,*y), (*x2,*y2));

let mut sorted_combo = vec![(*x,*y), (*x2,*y2)];
sorted_combo.sort();
if seen_combo.contains(&sorted_combo) {
continue;
}


dist += manhatten_distance((*x,*y), (*x2,*y2));
seen_combo.insert(sorted_combo);
}
dist as u64

}).sum::<u64>() / 2
//(I'm double counting)
}).sum::<u64>()

}

Expand Down

0 comments on commit 8f6a5ea

Please sign in to comment.