advent-of-code-2023/src/days/d11.rs
2023-12-13 00:26:48 +01:00

68 lines
2 KiB
Rust

use std::fs;
pub fn solve() {
let path = "res/11/input";
let contents = fs::read_to_string(path).expect("I/O error, wrong path?");
//let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
let mut galaxies: Vec<(usize, usize)> = Vec::new();
for (i, line) in contents.lines().enumerate() {
for (j, c) in line.chars().enumerate() {
if c == '#' {
galaxies.push((i, j))
}
}
}
let mut result = 0;
for i in 0..galaxies.len() {
for j in i + 1..galaxies.len() {
let (x1, y1) = galaxies[i];
let (x2, y2) = galaxies[j];
result += x1.abs_diff(x2) + y1.abs_diff(y2);
}
}
for (i, row) in contents.lines().enumerate() {
if !row.contains('#') {
let top = galaxies.iter().filter(|(x, _)| *x < i).count();
let bot = galaxies.len() - top;
result += top * bot;
}
}
let space: Vec<Vec<char>> = contents
.lines()
.map(|l| l.chars().collect::<Vec<_>>())
.collect();
for j in 0..space[0].len() {
let col: Vec<char> = (0..space.len()).map(|i| space[i][j]).collect();
if !col.contains(&'#') {
let left = galaxies.iter().filter(|(_, y)| *y < j).count();
let right = galaxies.len() - left;
result += right * left;
}
}
println!("Result 1: {result}");
for (i, row) in contents.lines().enumerate() {
if !row.contains('#') {
let top = galaxies.iter().filter(|(x, _)| *x < i).count();
let bot = galaxies.len() - top;
result += top * bot * 999998;
}
}
for j in 0..space[0].len() {
let col: Vec<char> = (0..space.len()).map(|i| space[i][j]).collect();
if !col.contains(&'#') {
let left = galaxies.iter().filter(|(_, y)| *y < j).count();
let right = galaxies.len() - left;
result += right * left * 999998;
}
}
println!("Result 2: {result}");
}