use std::fs; fn main() { let input = fs::read_to_string("input.txt").unwrap(); let input: Vec<_> = input.split('\n') .map(|x| x.chars().collect::>()) .collect(); let mut y_expand: Vec = vec![]; for line in &input { if line.iter().all(|char| char == &'.') { y_expand.push(1); } else { y_expand.push(0); } } let mut x_expand: Vec = vec![]; for j in 0..input[0].len() { let mut is_empty = true; for i in 0..input.len() { if input[i][j] != '.' { is_empty = false; } } if is_empty { x_expand.push(1); } else { x_expand.push(0); } } // println!("{:?}", x_expand); let mut galaxies: Vec<(i64, i64)> = vec![]; let mut y_offset: i64 = 0; for (i, line) in input.iter().enumerate() { if y_expand[i] == 1 { y_offset += 1000000 - 1; } let mut x_offset: i64 = 0; for (j, char) in line.iter().enumerate() { if x_expand[j] == 1 { x_offset += 1000000 - 1; } if char == &'#' { galaxies.push((i as i64 + y_offset, j as i64 + x_offset)); } } } let dist_total = galaxies.clone().into_iter().enumerate() .fold(0, |mut acc, (i, gal)| { for next in &galaxies[i + 1..] { acc += gal.0.abs_diff(next.0) + gal.1.abs_diff(next.1); } acc }); println!("{:?}", dist_total); } // use std::fs; // fn main() { // let input = fs::read_to_string("input.txt").unwrap(); // let input: Vec<_> = input.split('\n') // .map(|x| x.chars().collect::>()) // .collect(); // let mut rotate: Vec> = vec![]; // for j in 0..input[0].len() { // let mut tmp: Vec = vec![]; // for i in 0..input.len() { // tmp.push(input[i][j]); // } // if tmp.iter().all(|x| x == &'.') { // rotate.push(tmp.clone()); // } // rotate.push(tmp); // } // let mut expanded: Vec> = vec![]; // for j in 0..rotate[0].len() { // let mut tmp: Vec = vec![]; // for i in 0..rotate.len() { // tmp.push(rotate[i][j]); // } // if tmp.iter().all(|x| x == &'.') { // expanded.push(tmp.clone()); // } // expanded.push(tmp); // } // let mut galaxies: Vec<(i32, i32)> = vec![]; // for (i, line) in expanded.iter().enumerate() { // for (j, char) in line.iter().enumerate() { // if char == &'#' { // galaxies.push((i as i32,j as i32)); // } // } // } // let dist_total = galaxies.clone().into_iter().enumerate() // .fold(0, |mut acc, (i, gal)| { // for next in &galaxies[i + 1..] { // acc += gal.0.abs_diff(next.0) + gal.1.abs_diff(next.1); // } // acc // }); // println!("{:?}", dist_total); // }