]> aoc.elinar.fr Git - aoc_2023/commitdiff
Day11 - part 2
authoralex <null>
Mon, 11 Dec 2023 09:10:30 +0000 (10:10 +0100)
committeralex <null>
Mon, 11 Dec 2023 09:10:30 +0000 (10:10 +0100)
src/day11.rs

index 759a9bbb1cb944bc75a6a156f186161beec63300..f2241c71843a4b5ab0d6a2288d8d58afbeaf38d4 100644 (file)
@@ -16,8 +16,8 @@ pub fn run(input_file: &str) -> Result<(), Box<dyn Error>> {
     Ok(())
 }
 
-fn dst(a: (usize, usize), b: (usize, usize)) -> u32 {
-    (a.0.abs_diff(b.0) + a.1.abs_diff(b.1)) as u32
+fn dst(a: (usize, usize), b: (usize, usize)) -> u64 {
+    (a.0.abs_diff(b.0) + a.1.abs_diff(b.1)) as u64
 }
 
 #[derive(Debug)]
@@ -28,7 +28,7 @@ struct Puzzle {
 }
 
 impl Puzzle {
-    pub fn new_with_exapansion(input: &str) -> Self {
+    pub fn new_with_expansion(input: &str, expansion: usize) -> Self {
         let mut width = 0;
         let mut height = 0;
 
@@ -52,7 +52,7 @@ impl Puzzle {
                 row += 1;
                 // prise en compte de l'expansion en ligne
                 if !l.contains('#') {
-                    row += 1;
+                    row += expansion;
                 }
                 galaxies
             })
@@ -66,7 +66,7 @@ impl Puzzle {
             let tmp = coord.iter()
                 .filter(|c| c.1 == i);
             if tmp.clone().count() == 0 {
-                col += 1;
+                col += expansion;
             } else {
                 tmp.for_each(|c| { new_coord.push((c.0, c.1 + col)); });
             }
@@ -78,33 +78,39 @@ impl Puzzle {
             height,
         }
     }
+
+    pub fn sum_dst(self) -> u64 {
+        let coord = self.coord;
+        let n = coord.len();
+        let res: u64 = coord.iter().enumerate()
+            .map(|(i, &a)| {
+                let mut sum: u64 = 0;
+                for j in (i + 1)..n {
+                    //println!("a({}, {}) - b({},{})", a.0, a.1, coord[j].0, coord[j].1);
+                    sum += dst(a, coord[j]);
+                }
+                // retourner la sum des distances
+                sum
+            })
+            .sum();
+        res
+    }
 }
 
-fn run_part1(input: &str) -> Result<u32, Box<dyn Error>> {
+fn run_part1(input: &str) -> Result<u64, Box<dyn Error>> {
     println!("Running day11 - part 1");
 
-    let puzzle = Puzzle::new_with_exapansion(input);
-    let coord = puzzle.coord;
-
-    let n = coord.len();
-    let res: u32 = coord.iter().enumerate()
-        .map(|(i, &a)| {
-            let mut sum: u32 = 0;
-            for j in (i + 1)..n {
-                //println!("a({}, {}) - b({},{})", a.0, a.1, coord[j].0, coord[j].1);
-                sum += dst(a, coord[j]);
-            }
-            // retourner la sum des distances
-            sum
-        })
-        .sum();
-
+    let puzzle = Puzzle::new_with_expansion(input, 1);
+    let res = puzzle.sum_dst();
     Ok(res)
 }
 
-fn run_part2(input: &str) -> Result<u32, Box<dyn Error>> {
+fn run_part2(input: &str) -> Result<u64, Box<dyn Error>> {
     println!("Running day11 - part 2");
-    let res = 0;
+
+    let puzzle = Puzzle::new_with_expansion(input, 999999);
+    let res = puzzle.sum_dst();
+
     Ok(res)
 }
 
@@ -132,8 +138,16 @@ mod tests {
     }
 
     #[test]
-    fn day11_part2() {
-        let res = run_part2(TEXT_INPUT);
-        assert_eq!(0, res.unwrap());
+    fn day11_part2_example1() {
+        let puzzle = Puzzle::new_with_expansion(TEXT_INPUT, 9);
+        let res = puzzle.sum_dst();
+        assert_eq!(1030, res);
+    }
+
+    #[test]
+    fn day11_part2_example2() {
+        let puzzle = Puzzle::new_with_expansion(TEXT_INPUT, 99);
+        let res = puzzle.sum_dst();
+        assert_eq!(8410, res);
     }
 }