Skip to content

Commit

Permalink
submit day 11 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
dcastil committed Dec 17, 2023
1 parent c3a3f2b commit 994c33c
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions src/bin/11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,13 @@ advent_of_code::solution!(11);
pub fn part_one(input: &str) -> Option<usize> {
let grid = GalaxyGrid::from_input(input);

let mut processed_galaxies = Vec::new();
let mut distances_sum = 0;

for galaxy in grid.galaxies() {
for processed_galaxy in processed_galaxies.iter() {
distances_sum += galaxy.distance(processed_galaxy);
}

processed_galaxies.push(galaxy);
}

Some(distances_sum)
Some(grid.sum_of_galaxy_distances(2))
}

pub fn part_two(input: &str) -> Option<u32> {
None
pub fn part_two(input: &str) -> Option<usize> {
let grid = GalaxyGrid::from_input(input);

Some(grid.sum_of_galaxy_distances(1_000_000))
}

struct GalaxyGrid<'a> {
Expand All @@ -34,7 +25,22 @@ impl GalaxyGrid<'_> {
}
}

fn galaxies(&self) -> Vec<Galaxy> {
fn sum_of_galaxy_distances(&self, expansion_factor: usize) -> usize {
let mut processed_galaxies = Vec::new();
let mut distances_sum = 0;

for galaxy in self.galaxies(expansion_factor) {
for processed_galaxy in processed_galaxies.iter() {
distances_sum += galaxy.distance(processed_galaxy);
}

processed_galaxies.push(galaxy);
}

distances_sum
}

fn galaxies(&self, expansion_factor: usize) -> Vec<Galaxy> {
let mut galaxies = Vec::new();
let xs_without_galaxies: HashSet<_> = self.xs_without_galaxies().collect();

Expand All @@ -46,7 +52,7 @@ impl GalaxyGrid<'_> {

for (x_index, character) in line.as_bytes().iter().enumerate() {
if xs_without_galaxies.contains(&x_index) {
current_x += 2;
current_x += expansion_factor;
continue;
}

Expand All @@ -59,7 +65,7 @@ impl GalaxyGrid<'_> {
current_x += 1;
}

current_y += if y_has_galaxies { 1 } else { 2 };
current_y += if y_has_galaxies { 1 } else { expansion_factor };
}

galaxies
Expand Down

0 comments on commit 994c33c

Please sign in to comment.