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::fmt::{Display, Write};
|
||||||
use std::iter::repeat;
|
use std::iter::repeat;
|
||||||
|
|
||||||
|
const P2_EXPANSION_FACTOR: u64 = 1000000;
|
||||||
|
|
||||||
// PARSE
|
// PARSE
|
||||||
|
|
||||||
struct GalaxyMap {
|
struct GalaxyMap {
|
||||||
@@ -93,7 +95,7 @@ impl GalaxyMap {
|
|||||||
|
|
||||||
x_dist as u64 + y_dist as u64
|
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 (a_x, a_y) = self.galaxy_positions[a];
|
||||||
let (b_x, b_y) = self.galaxy_positions[b];
|
let (b_x, b_y) = self.galaxy_positions[b];
|
||||||
let x_dist = b_x.abs_diff(a_x);
|
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) {
|
for row in a_y.min(b_y)..a_y.max(b_y) {
|
||||||
if self.map[row].iter().all(|c| *c == 'x') {
|
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) {
|
for col in a_x.min(b_x)..a_x.max(b_x) {
|
||||||
if self.map.iter().all(|row| row[col] == '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
|
sum
|
||||||
}
|
}
|
||||||
|
|
||||||
// PROBLEM 2 solution
|
fn part2_impl(input: &str, factor: u64) -> u64 {
|
||||||
#[aoc(day11, part2)]
|
|
||||||
fn part2(input: &str) -> u64 {
|
|
||||||
let mut map = GalaxyMap::from(input);
|
let mut map = GalaxyMap::from(input);
|
||||||
map.universe_expansion2();
|
map.universe_expansion2();
|
||||||
map.find_galaxies();
|
map.find_galaxies();
|
||||||
@@ -162,12 +162,18 @@ fn part2(input: &str) -> u64 {
|
|||||||
while let Some(target) = galaxies.pop() {
|
while let Some(target) = galaxies.pop() {
|
||||||
sum += galaxies
|
sum += galaxies
|
||||||
.iter()
|
.iter()
|
||||||
.map(|source| map.galaxy_distance2(*source, target))
|
.map(|source| map.galaxy_distance2(*source, target, factor))
|
||||||
.sum::<u64>();
|
.sum::<u64>();
|
||||||
}
|
}
|
||||||
sum
|
sum
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PROBLEM 2 solution
|
||||||
|
#[aoc(day11, part2)]
|
||||||
|
fn part2(input: &str) -> u64 {
|
||||||
|
part2_impl(input, P2_EXPANSION_FACTOR)
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -190,6 +196,6 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn part2_example() {
|
fn part2_example() {
|
||||||
assert_eq!(part2(EXAMPLE), 0);
|
assert_eq!(part2_impl(EXAMPLE, 100), 8410);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user