chore: fmt / clippy

This commit is contained in:
Xavier Morel
2024-12-15 18:48:59 +01:00
parent dbaabee656
commit 57cfca7c7b

View File

@@ -1,8 +1,6 @@
advent_of_code::solution!(12); advent_of_code::solution!(12);
use itertools::Itertools;
use std::collections::HashMap; use std::collections::HashMap;
use std::iter::successors;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct Pos { struct Pos {
@@ -90,9 +88,7 @@ impl Region {
} }
fn area(&self) -> usize { fn area(&self) -> usize {
let area = self.ps.len(); self.ps.len()
// println!("Region {:?} -> area: {:?}", self.c, area);
area
} }
fn perimeter(&self) -> usize { fn perimeter(&self) -> usize {
@@ -159,7 +155,7 @@ impl Region {
let l = match ps.len() { let l = match ps.len() {
2 => { 2 => {
match ps match ps
.into_iter() .iter()
.fold(Pos::origin(), |acc, p2| acc.add(&p.diff(p2))) .fold(Pos::origin(), |acc, p2| acc.add(&p.diff(p2)))
{ {
Pos { x: 0, y: 0 } => 0, Pos { x: 0, y: 0 } => 0,
@@ -170,9 +166,9 @@ impl Region {
4 => 4, 4 => 4,
_ => 0, _ => 0,
}; };
let neighbors_vec = ps // let neighbors_vec = ps
.into_iter() // .iter()
.fold(Pos::origin(), |acc, p2| acc.add(&p.diff(p2))); // .fold(Pos::origin(), |acc, p2| acc.add(&p.diff(p2)));
// println!(" * Outward angle(s) around: {:?} -> {}", p, l); // println!(" * Outward angle(s) around: {:?} -> {}", p, l);
// println!(" Neighbors vec: {:?}", neighbors_vec); // println!(" Neighbors vec: {:?}", neighbors_vec);
outward_angles += l; outward_angles += l;
@@ -184,14 +180,12 @@ impl Region {
let inward_angles = neighbors_map let inward_angles = neighbors_map
.values() .values()
.map(|ps| { .map(|ps| {
let l = match ps.len() { match ps.len() {
2 => 1, 2 => 1,
3 => 2, 3 => 2,
4 => 4, 4 => 4,
_ => 0, _ => 0,
}; }
// println!(" * Inward angle(s) around: ?? -> {}", l);
l
}) })
.sum::<usize>(); .sum::<usize>();
// println!("Region {:} -> inward angles: {}, outward angles: {}", self.c, inward_angles, outward_angles); // println!("Region {:} -> inward angles: {}, outward angles: {}", self.c, inward_angles, outward_angles);
@@ -200,49 +194,49 @@ impl Region {
} }
} }
fn count_sides(path: &mut Vec<Pos>) -> usize { // fn count_sides(path: &mut Vec<Pos>) -> usize {
if path.len() <= 1 { // if path.len() <= 1 {
return 0; // return 0;
} // }
let start = path.remove(0); // let start = path.remove(0);
let directions = [ // let directions = [
Pos { x: 0, y: 1 }, // up // Pos { x: 0, y: 1 }, // up
Pos { x: 1, y: 0 }, // right // Pos { x: 1, y: 0 }, // right
Pos { x: 0, y: -1 }, // down // Pos { x: 0, y: -1 }, // down
Pos { x: -1, y: 0 }, // left // Pos { x: -1, y: 0 }, // left
]; // ];
let steps = successors(Some((start, 1, None)), |(cur, sides, d_idx)| { // let steps = successors(Some((start, 1, None)), |(cur, sides, d_idx)| {
// println!("Current: {:?}", cur); // // println!("Current: {:?}", cur);
if path.is_empty() { // if path.is_empty() {
return None; // return None;
} // }
for i in 0..4 { // for i in 0..4 {
let dir = directions[(d_idx.unwrap_or(0) + i) % 4].clone(); // let dir = directions[(d_idx.unwrap_or(0) + i) % 4].clone();
let next = cur.add(&dir); // let next = cur.add(&dir);
if let Some(ofs) = path.iter().position(|p| p == &next) { // if let Some(ofs) = path.iter().position(|p| p == &next) {
// println!(" Trying direction {:?} -> {:?} (Found!)", dir, next); // // println!(" Trying direction {:?} -> {:?} (Found!)", dir, next);
let new_cur = path.remove(ofs); // let new_cur = path.remove(ofs);
let new_sides = sides // let new_sides = sides
+ if d_idx.is_some() && i > 0 { // + if d_idx.is_some() && i > 0 {
// println!("(new direction, incrementing sides: {:?})", sides + 1); // // println!("(new direction, incrementing sides: {:?})", sides + 1);
1 // 1
} else { // } else {
// println!("(same direction, sides: {:?})", sides); // // println!("(same direction, sides: {:?})", sides);
0 // 0
}; // };
return Some((new_cur, new_sides, Some(d_idx.unwrap_or(0) + i))); // return Some((new_cur, new_sides, Some(d_idx.unwrap_or(0) + i)));
// } else { // // } else {
// println!(" Trying direction {:?} -> {:?} (Not found)", dir, next); // // println!(" Trying direction {:?} -> {:?} (Not found)", dir, next);
} // }
} // }
let new_cur = path.remove(0); // let new_cur = path.remove(0);
// println!("No consecutive neighbor found -> Trying new side (sides: {})", sides + 1); // // println!("No consecutive neighbor found -> Trying new side (sides: {})", sides + 1);
Some((new_cur, sides + 1, None)) // Some((new_cur, sides + 1, None))
}) // })
.collect::<Vec<_>>(); // .collect::<Vec<_>>();
steps.last().unwrap().1 // steps.last().unwrap().1
} // }
fn parse_input(input: &str) -> Vec<Region> { fn parse_input(input: &str) -> Vec<Region> {
let mut res: Vec<Region> = Vec::new(); let mut res: Vec<Region> = Vec::new();