diff --git a/src/11part1.rs b/src/11part1.rs new file mode 100644 index 0000000..b00e49c --- /dev/null +++ b/src/11part1.rs @@ -0,0 +1,86 @@ +use std::fs; + +#[derive(Eq, PartialEq, Hash, Copy, Clone, Debug)] +struct Coordinate { + x: usize, + y: usize, +} + +fn main() { + let file_path = "input/11input.txt"; + let contents = fs::read_to_string(file_path).expect("should read file"); + let mut lines = contents.lines(); + let mut matrix: Vec> = Vec::new(); + loop { + match lines.next() { + Some(line) => { + matrix.push(line.chars().collect()); + }, + None => break, + } + } + let mut matrix_expanded: Vec> = Vec::new(); + let mut matrix_expanded_full: Vec> = Vec::new(); + for y in 0..matrix.len() { + let mut has_galaxy = false; + for x in 0..matrix[y].len() { + has_galaxy = has_galaxy || matrix[y][x] == '#'; + } + if !has_galaxy { + matrix_expanded.push(matrix[y].to_owned()); + matrix_expanded.push(matrix[y].to_owned()); + matrix_expanded_full.push(Vec::new()); + matrix_expanded_full.push(Vec::new()); + } else { + matrix_expanded.push(matrix[y].to_owned()); + matrix_expanded_full.push(Vec::new()); + } + } + for x in 0..matrix_expanded[0].len() { + let mut has_galaxy = false; + for y in 0..matrix_expanded.len() { + has_galaxy = has_galaxy || matrix_expanded[y][x] == '#'; + } + if has_galaxy { + for y in 0..matrix_expanded.len() { + matrix_expanded_full[0].push(matrix_expanded[y][x]); + matrix_expanded_full.push(matrix_expanded_full[0].to_owned()); + matrix_expanded_full.remove(0); + } + } else { + for _y in 0..matrix_expanded.len() { + matrix_expanded_full[0].push('.'); + matrix_expanded_full[0].push('.'); + matrix_expanded_full.push(matrix_expanded_full[0].to_owned()); + matrix_expanded_full.remove(0); + } + } + } + let mut coordinates = Vec::new(); + for y in 0..matrix_expanded_full.len() { + for x in 0..matrix_expanded_full[y].len() { + if matrix_expanded_full[y][x] == '#' { + coordinates.push(Coordinate{x, y}); + } + } + } + let mut sum = 0; + for a in 0..coordinates.len()-1 { + for b in a+1..coordinates.len() { + if coordinates[a].x > coordinates[b].x { + if coordinates[a].y > coordinates[b].y { + sum += coordinates[a].x - coordinates[b].x + coordinates[a].y - coordinates[b].y; + } else { + sum += coordinates[a].x - coordinates[b].x + coordinates[b].y - coordinates[a].y; + } + } else { + if coordinates[a].y > coordinates[b].y { + sum += coordinates[b].x - coordinates[a].x + coordinates[a].y - coordinates[b].y; + } else { + sum += coordinates[b].x - coordinates[a].x + coordinates[b].y - coordinates[a].y; + } + } + } + } + println!("{}", sum); +} diff --git a/src/11part2.rs b/src/11part2.rs new file mode 100644 index 0000000..bbcc64d --- /dev/null +++ b/src/11part2.rs @@ -0,0 +1,77 @@ +use std::fs; + +#[derive(Eq, PartialEq, Hash, Copy, Clone, Debug)] +struct Coordinate { + x: usize, + y: usize, +} + +fn main() { + let file_path = "input/11input.txt"; + let contents = fs::read_to_string(file_path).expect("should read file"); + let mut lines = contents.lines(); + let mut matrix: Vec> = Vec::new(); + loop { + match lines.next() { + Some(line) => { + matrix.push(line.chars().collect()); + }, + None => break, + } + } + let mut coordinates = Vec::new(); + let mut coordinates_copy = Vec::new(); + for y in 0..matrix.len() { + for x in 0..matrix[y].len() { + if matrix[y][x] == '#' { + coordinates.push(Coordinate{x, y}); + coordinates_copy.push(Coordinate{x, y}); + } + } + } + for y in 0..matrix.len() { + let mut has_galaxy = false; + for x in 0..matrix[y].len() { + has_galaxy = has_galaxy || matrix[y][x] == '#'; + } + if !has_galaxy { + for c in 0..coordinates.len() { + if coordinates[c].y > y { + coordinates_copy[c].y += 999999; + } + } + } + } + for x in 0..matrix[0].len() { + let mut has_galaxy = false; + for y in 0..matrix.len() { + has_galaxy = has_galaxy || matrix[y][x] == '#'; + } + if !has_galaxy { + for c in 0..coordinates.len() { + if coordinates[c].x > x { + coordinates_copy[c].x += 999999; + } + } + } + } + let mut sum = 0; + for a in 0..coordinates_copy.len()-1 { + for b in a+1..coordinates_copy.len() { + if coordinates_copy[a].x > coordinates_copy[b].x { + if coordinates_copy[a].y > coordinates_copy[b].y { + sum += coordinates_copy[a].x - coordinates_copy[b].x + coordinates_copy[a].y - coordinates_copy[b].y; + } else { + sum += coordinates_copy[a].x - coordinates_copy[b].x + coordinates_copy[b].y - coordinates_copy[a].y; + } + } else { + if coordinates_copy[a].y > coordinates_copy[b].y { + sum += coordinates_copy[b].x - coordinates_copy[a].x + coordinates_copy[a].y - coordinates_copy[b].y; + } else { + sum += coordinates_copy[b].x - coordinates_copy[a].x + coordinates_copy[b].y - coordinates_copy[a].y; + } + } + } + } + println!("{}", sum); +}