feat: day 9 p2 + fmt / clippy

This commit is contained in:
Xavier Morel
2024-12-10 00:17:46 +01:00
parent 2825fdd2ca
commit 1639af81fc
2 changed files with 151 additions and 74 deletions

View File

@@ -15,14 +15,14 @@ impl Pos {
Self { x, y }
}
fn diff(&self, other: &Self) -> Self {
fn diff(self, other: Self) -> Self {
Self {
x: other.x - self.x,
y: other.y - self.y,
}
}
fn add(&self, other: &Self) -> Self {
fn add(self, other: Self) -> Self {
Self {
x: self.x + other.x,
y: self.y + other.y,
@@ -30,16 +30,15 @@ impl Pos {
}
fn is_valid(&self, max: &Self) -> bool {
self.x >= 0 && self.x < max.x
&& self.y >= 0 && self.y < max.y
self.x >= 0 && self.x < max.x && self.y >= 0 && self.y < max.y
}
}
fn get_antinodes(p1: &Pos, p2: &Pos, max: &Pos) -> Vec<Pos> {
let d1 = p1.diff(&p2);
let d2 = p2.diff(&p1);
let p1b = p1.add(&d2);
let p2b = p2.add(&d1);
let d1 = p1.diff(*p2);
let d2 = p2.diff(*p1);
let p1b = p1.add(d2);
let p2b = p2.add(d1);
let mut res: Vec<Pos> = Vec::new();
if p1b.is_valid(max) {
res.push(p1b);
@@ -50,31 +49,27 @@ fn get_antinodes(p1: &Pos, p2: &Pos, max: &Pos) -> Vec<Pos> {
res
}
fn get_antinodes_p2(p1: &Pos, p2: &Pos, max: &Pos) -> Vec<Pos> {
let d1 = p2.diff(&p1);
let mut succ1: Vec<Pos> = successors(
Some(*p1),
|p| {
let s = p.add(&d1);
if s.is_valid(max) {
Some(s)
} else {
None
}
fn get_antinodes_p2(p1: Pos, p2: Pos, max: &Pos) -> Vec<Pos> {
let d1 = p2.diff(p1);
let mut succ1: Vec<Pos> = successors(Some(p1), |p| {
let s = p.add(d1);
if s.is_valid(max) {
Some(s)
} else {
None
}
).collect();
let d2 = p1.diff(&p2);
let succ2: Vec<Pos> = successors(
Some(*p2),
|p| {
let s = p.add(&d2);
if s.is_valid(max) {
Some(s)
} else {
None
}
})
.collect();
let d2 = p1.diff(p2);
let succ2: Vec<Pos> = successors(Some(p2), |p| {
let s = p.add(d2);
if s.is_valid(max) {
Some(s)
} else {
None
}
).collect();
})
.collect();
succ1.extend(succ2);
succ1.into_iter().map(|p| p.to_owned()).collect()
}
@@ -113,7 +108,7 @@ fn parse_line(line: &str, y: usize) -> Vec<(char, Pos)> {
// }
// }
fn parse_input(input: &str) -> (Pos, HashMap::<char, Vec<Pos>>) {
fn parse_input(input: &str) -> (Pos, HashMap<char, Vec<Pos>>) {
let mut antenna: HashMap<char, Vec<Pos>> = HashMap::new();
let lines = input.lines().collect::<Vec<_>>();
let map_size = Pos::init(lines[0].len() as i8, lines.len() as i8);
@@ -122,7 +117,7 @@ fn parse_input(input: &str) -> (Pos, HashMap::<char, Vec<Pos>>) {
.enumerate()
.flat_map(|(y, l)| parse_line(l, y))
.for_each(|(c, p)| {
antenna.entry(c).or_insert(Vec::new()).push(p);
antenna.entry(c).or_default().push(p);
});
(map_size, antenna)
}
@@ -131,11 +126,12 @@ pub fn part_one(input: &str) -> Option<usize> {
let (map_size, antenna) = parse_input(input);
let mut antinodes: Vec<Pos> = antenna
.values()
.flat_map(|ps| ps.into_iter()
.combinations(2)
.flat_map(|a| get_antinodes(a[0], a[1], &map_size))
.collect::<Vec<_>>()
)
.flat_map(|ps| {
ps.iter()
.combinations(2)
.flat_map(|a| get_antinodes(a[0], a[1], &map_size))
.collect::<Vec<_>>()
})
.collect();
antinodes.sort();
antinodes.dedup();
@@ -146,11 +142,12 @@ pub fn part_two(input: &str) -> Option<usize> {
let (map_size, antenna) = parse_input(input);
let mut antinodes: Vec<Pos> = antenna
.values()
.flat_map(|ps| ps.into_iter()
.combinations(2)
.flat_map(|a| get_antinodes_p2(a[0], a[1], &map_size))
.collect::<Vec<_>>()
)
.flat_map(|ps| {
ps.iter()
.combinations(2)
.flat_map(|a| get_antinodes_p2(*a[0], *a[1], &map_size))
.collect::<Vec<_>>()
})
.collect();
antinodes.sort();
antinodes.dedup();