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)]
}
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;
row += 1;
// prise en compte de l'expansion en ligne
if !l.contains('#') {
- row += 1;
+ row += expansion;
}
galaxies
})
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)); });
}
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)
}
}
#[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);
}
}