summaryrefslogtreecommitdiff
path: root/src/bin/day11.rs
diff options
context:
space:
mode:
authorReiner Herrmann <reiner@reiner-h.de>2023-12-11 15:06:10 +0100
committerReiner Herrmann <reiner@reiner-h.de>2023-12-11 15:06:10 +0100
commit1cae5b888ca3a82b99f4dec64911ab1a7c27471c (patch)
tree04c090e0624bbe1e89e170cb6a350d4aaf4ee4e3 /src/bin/day11.rs
parent2007f5bc107cfb7458ffab3a728b83f236c606d2 (diff)
day11 solution 2
Diffstat (limited to 'src/bin/day11.rs')
-rw-r--r--src/bin/day11.rs22
1 files changed, 12 insertions, 10 deletions
diff --git a/src/bin/day11.rs b/src/bin/day11.rs
index ca5f481..be8ca90 100644
--- a/src/bin/day11.rs
+++ b/src/bin/day11.rs
@@ -2,8 +2,8 @@ static DAY: u8 = 11;
fn main() {
let input = advent::read_lines(DAY);
- println!("{DAY}a: {}", sum_path_lengths(&input));
- println!("{DAY}b: {}", 0);
+ println!("{DAY}a: {}", sum_path_lengths(&input, 2));
+ println!("{DAY}b: {}", sum_path_lengths(&input, 1_000_000));
}
#[derive(PartialEq, Eq)]
@@ -34,7 +34,7 @@ impl GalaxyMap {
GalaxyMap { galaxies, width, height }
}
- fn expand_space(&mut self) {
+ fn expand_space(&mut self, factor: isize) {
let mut columns = Vec::new();
for x in 0 .. self.width {
if !self.galaxies.iter().any(|pos| pos.x == x) {
@@ -49,15 +49,15 @@ impl GalaxyMap {
}
for x in columns.iter().rev() {
for galaxy in self.galaxies.iter_mut().filter(|pos| pos.x > *x) {
- galaxy.x += 1;
+ galaxy.x += factor - 1;
}
- self.width += 1;
+ self.width += factor - 1;
}
for y in rows.iter().rev() {
for galaxy in self.galaxies.iter_mut().filter(|pos| pos.y > *y) {
- galaxy.y += 1;
+ galaxy.y += factor - 1;
}
- self.height += 1;
+ self.height += factor - 1;
}
}
@@ -86,9 +86,9 @@ impl GalaxyMap {
}
}
-fn sum_path_lengths(input: &[String]) -> isize {
+fn sum_path_lengths(input: &[String], factor: isize) -> isize {
let mut galaxymap = GalaxyMap::new(input);
- galaxymap.expand_space();
+ galaxymap.expand_space(factor);
galaxymap.calculate_distances()
.iter()
.sum()
@@ -112,6 +112,8 @@ mod tests {
".......#..",
"#...#.....",
].iter().map(|&x| String::from(x)).collect::<Vec<_>>();
- assert_eq!(sum_path_lengths(&input), 374);
+ assert_eq!(sum_path_lengths(&input, 2), 374);
+ assert_eq!(sum_path_lengths(&input, 10), 1030);
+ assert_eq!(sum_path_lengths(&input, 100), 8410);
}
}