2023/day11: refactor factor to fix test
This commit is contained in:
1 file changed
+14
-8
+14
-8
@@ -2,6 +2,8 @@ use aoc_runner_derive::aoc;
|
||||
use std::fmt::{Display, Write};
|
||||
use std::iter::repeat;
|
||||
|
||||
const P2_EXPANSION_FACTOR: u64 = 1000000;
|
||||
|
||||
// PARSE
|
||||
|
||||
struct GalaxyMap {
|
||||
@@ -93,7 +95,7 @@ impl GalaxyMap {
|
||||
|
||||
x_dist as u64 + y_dist as u64
|
||||
}
|
||||
fn galaxy_distance2(&self, a: usize, b: usize) -> 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);
|
||||
@@ -103,13 +105,13 @@ impl GalaxyMap {
|
||||
|
||||
for row in a_y.min(b_y)..a_y.max(b_y) {
|
||||
if self.map[row].iter().all(|c| *c == 'x') {
|
||||
dist += 1000000 - 1;
|
||||
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 += 1000000 - 1;
|
||||
dist += factor - 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,9 +151,7 @@ fn part1(input: &str) -> u64 {
|
||||
sum
|
||||
}
|
||||
|
||||
// PROBLEM 2 solution
|
||||
#[aoc(day11, part2)]
|
||||
fn part2(input: &str) -> u64 {
|
||||
fn part2_impl(input: &str, factor: u64) -> u64 {
|
||||
let mut map = GalaxyMap::from(input);
|
||||
map.universe_expansion2();
|
||||
map.find_galaxies();
|
||||
@@ -162,12 +162,18 @@ fn part2(input: &str) -> u64 {
|
||||
while let Some(target) = galaxies.pop() {
|
||||
sum += galaxies
|
||||
.iter()
|
||||
.map(|source| map.galaxy_distance2(*source, target))
|
||||
.map(|source| map.galaxy_distance2(*source, target, factor))
|
||||
.sum::<u64>();
|
||||
}
|
||||
sum
|
||||
}
|
||||
|
||||
// PROBLEM 2 solution
|
||||
#[aoc(day11, part2)]
|
||||
fn part2(input: &str) -> u64 {
|
||||
part2_impl(input, P2_EXPANSION_FACTOR)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -190,6 +196,6 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn part2_example() {
|
||||
assert_eq!(part2(EXAMPLE), 0);
|
||||
assert_eq!(part2_impl(EXAMPLE, 100), 8410);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user