day 11 part 2

This commit is contained in:
Dominic 2023-12-11 11:13:02 +01:00
parent c6fc7ded50
commit bb03ac3dca
Signed by: msrd0
GPG key ID: DCC8C247452E98F9

View file

@ -27,6 +27,39 @@ fn parser() -> impl Parser<char, Vec<Vec<Cell>>, Error = Simple<char>> {
.then_ignore(end())
}
fn sum_shortest_paths(
empty_rows: &HashSet<usize>,
empty_cols: &HashSet<usize>,
galaxies: &[(usize, usize)],
multiplier: u64
) -> u64 {
let mut sum = 0;
for (i, g) in galaxies.iter().enumerate() {
for (_j, h) in galaxies.iter().enumerate().skip(i + 1) {
let mut x = [g.0, h.0];
x.sort_unstable();
let mut dist = (x[0] .. x[1])
.map(|row| match empty_rows.contains(&row) {
true => multiplier,
false => 1
})
.sum::<u64>();
let mut y = [g.1, h.1];
y.sort_unstable();
dist += (y[0] .. y[1])
.map(|col| match empty_cols.contains(&col) {
true => multiplier,
false => 1
})
.sum::<u64>();
// eprintln!(" - Between galaxy {} and galaxy {}: {dist}", i + 1, _j + 1);
sum += dist;
}
}
sum
}
fn main() -> anyhow::Result<()> {
let universe = read("inputs/day11.txt", parser())?;
@ -53,31 +86,14 @@ fn main() -> anyhow::Result<()> {
.map(|(i, j, _)| (i, j))
.collect();
let mut sum = 0_u64;
for (i, g) in galaxies.iter().enumerate() {
for (_j, h) in galaxies.iter().enumerate().skip(i + 1) {
let mut x = [g.0, h.0];
x.sort_unstable();
let mut dist = (x[0] .. x[1])
.map(|row| match empty_rows.contains(&row) {
true => 2,
false => 1
})
.sum::<u64>();
let mut y = [g.1, h.1];
y.sort_unstable();
dist += (y[0] .. y[1])
.map(|col| match empty_cols.contains(&col) {
true => 2,
false => 1
})
.sum::<u64>();
// eprintln!(" - Between galaxy {} and galaxy {}: {dist}", i + 1, _j + 1);
sum += dist;
}
}
println!("{sum}");
println!(
"{}",
sum_shortest_paths(&empty_rows, &empty_cols, &galaxies, 2)
);
println!(
"{}",
sum_shortest_paths(&empty_rows, &empty_cols, &galaxies, 1_000_000)
);
Ok(())
}