A small refactor

This commit is contained in:
Dennis Brentjes 2023-12-13 21:25:37 +01:00
parent 9cdb1c0dcf
commit 44d402849a

View File

@ -27,7 +27,7 @@ impl FromStr for Chart {
}
impl Chart {
fn detect_doubles(&mut self) {
fn detect_empties(&mut self) {
for x in 0..self.stride {
if self.space.iter().skip(x).step_by(self.stride).all(|c| *c == '.') {
self.empty_columns.insert(x);
@ -51,47 +51,43 @@ impl Chart {
vec
}
fn distance(&self, c1: &(usize, usize), c2: &(usize, usize), extra_distance: u64) -> u64 {
fn distance(&self, c1: &(usize, usize), c2: &(usize, usize), empty_distance: u64) -> u64 {
let mut steps = 0;
for x in (min(c1.0, c2.0)..=max(c1.0, c2.0)).into_iter().skip(1) {
steps += 1;
if self.empty_columns.contains(&x) {
steps += extra_distance;
steps += empty_distance;
} else {
steps += 1;
}
}
for y in (min(c1.1, c2.1)..=max(c1.1, c2.1)).into_iter().skip(1) {
steps += 1;
if (self.empty_rows).contains(&y) {
steps += extra_distance;
steps += empty_distance;
} else {
steps += 1;
}
}
steps
}
}
pub fn one() -> Result<u64, AdventError> {
pub fn solve(empty_distance: u64) -> Result<u64, AdventError> {
let mut chart = read_into::<Chart>(Path::new("resources/input11.txt"))?;
chart.detect_doubles();
chart.detect_empties();
let galaxy_coords = chart.find_galaxys();
let mut sum: u64 = 0;
for (i, c1) in galaxy_coords.iter().enumerate() {
for c2 in galaxy_coords.iter().skip(i+1) {
sum += chart.distance(c1, c2, 2-1);
sum += chart.distance(c1, c2, empty_distance);
}
}
Ok(sum as u64)
Ok(sum)
}
pub fn one() -> Result<u64, AdventError> {
solve(2)
}
pub fn two() -> Result<u64, AdventError> {
let mut chart = read_into::<Chart>(Path::new("resources/input11.txt"))?;
chart.detect_doubles();
let galaxy_coords = chart.find_galaxys();
let mut sum: u64 = 0;
for (i, c1) in galaxy_coords.iter().enumerate() {
for c2 in galaxy_coords.iter().skip(i+1) {
sum += chart.distance(c1, c2, 1000000-1);
}
}
Ok(sum as u64)
solve(1000000)
}