use aoc_runner_derive::aoc; use std::fmt::{Display, Write}; use std::iter::repeat; const P2_EXPANSION_FACTOR: u64 = 1000000; // PARSE struct GalaxyMap { map: Vec>, galaxy_positions: Vec<(usize, usize)>, } impl From<&str> for GalaxyMap { fn from(input: &str) -> Self { let map = GalaxyMap { map: input.lines().map(|line| line.chars().collect()).collect(), galaxy_positions: Vec::new(), }; map } } impl GalaxyMap { fn universe_expansion(&mut self) { let mut columns_to_insert = Vec::new(); for col in 0..self.map[0].len() { if self.map.iter().map(|row| row[col]).all(|c| c != '#') { columns_to_insert.push(col); } } for row in &mut self.map { let mut offset = 0; for col in &columns_to_insert { row.insert(*col + offset, '.'); offset += 1; } } let rows_to_insert: Vec<_> = self .map .iter() .enumerate() .filter_map(|(idx, row)| if !row.contains(&'#') { Some(idx) } else { None }) .collect(); let gen_row: Vec<_> = repeat('.').take(self.map[0].len()).collect(); let mut offset = 0; for idx in rows_to_insert { self.map.insert(idx + offset, gen_row.clone()); offset += 1; } } fn universe_expansion2(&mut self) { let mut columns_to_grow = Vec::new(); for col in 0..self.map[0].len() { if self.map.iter().map(|row| row[col]).all(|c| c != '#') { columns_to_grow.push(col); } } for row in &mut self.map { for col in &columns_to_grow { row[*col] = 'x'; } } let rows_to_grow: Vec<_> = self .map .iter() .enumerate() .filter_map(|(idx, row)| if !row.contains(&'#') { Some(idx) } else { None }) .collect(); let gen_row: Vec<_> = repeat('x').take(self.map[0].len()).collect(); for idx in rows_to_grow { self.map[idx] = gen_row.clone(); } } fn find_galaxies(&mut self) { for (y, row) in self.map.iter().enumerate() { for (x, _c) in row.iter().enumerate().filter(|(_x, c)| **c == '#') { self.galaxy_positions.push((x, y)); } } } fn galaxy_distance(&self, a: usize, b: usize) -> u64 { let (a_x, a_y) = self.galaxy_positions[a]; let (b_x, b_y) = self.galaxy_positions[b]; let x_dist = b_x.abs_diff(a_x); let y_dist = b_y.abs_diff(a_y); x_dist as u64 + y_dist as u64 } fn galaxy_distance2(&self, a: usize, b: usize, factor: u64) -> u64 { let (a_x, a_y) = self.galaxy_positions[a]; let (b_x, b_y) = self.galaxy_positions[b]; let x_dist = b_x.abs_diff(a_x); let y_dist = b_y.abs_diff(a_y); let mut dist = x_dist as u64 + y_dist as u64; for row in a_y.min(b_y)..a_y.max(b_y) { if self.map[row].iter().all(|c| *c == 'x') { dist += factor - 1; } } for col in a_x.min(b_x)..a_x.max(b_x) { if self.map.iter().all(|row| row[col] == 'x') { dist += factor - 1; } } dist } } impl Display for GalaxyMap { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { for row in &self.map { for c in row { f.write_char(*c)?; } f.write_char('\n')?; } Ok(()) } } // PROBLEM 1 solution #[aoc(day11, part1)] fn part1(input: &str) -> u64 { let mut map = GalaxyMap::from(input); map.universe_expansion(); map.find_galaxies(); // println!("{}", map); let mut galaxies: Vec<_> = (0..map.galaxy_positions.len()).collect(); let mut sum = 0u64; while let Some(target) = galaxies.pop() { sum += galaxies .iter() .map(|source| map.galaxy_distance(*source, target)) .sum::(); } sum } fn part2_impl(input: &str, factor: u64) -> u64 { let mut map = GalaxyMap::from(input); map.universe_expansion2(); map.find_galaxies(); // println!("{}", map); let mut galaxies: Vec<_> = (0..map.galaxy_positions.len()).collect(); let mut sum = 0u64; while let Some(target) = galaxies.pop() { sum += galaxies .iter() .map(|source| map.galaxy_distance2(*source, target, factor)) .sum::(); } sum } // PROBLEM 2 solution #[aoc(day11, part2)] fn part2(input: &str) -> u64 { part2_impl(input, P2_EXPANSION_FACTOR) } #[cfg(test)] mod tests { use super::*; const EXAMPLE: &str = "...#...... .......#.. #......... .......... ......#... .#........ .........# .......... .......#.. #...#....."; #[test] fn part1_example() { assert_eq!(part1(EXAMPLE), 374); } #[test] fn part2_example() { assert_eq!(part2_impl(EXAMPLE, 100), 8410); } }