chore: fmt

This commit is contained in:
Xavier Morel
2024-12-18 23:31:33 +01:00
parent 8e1f9c7ffc
commit bf1153dfaa
2 changed files with 29 additions and 16 deletions

View File

@@ -121,7 +121,8 @@ impl TryFrom<&str> for State {
// y: input.len(),
// };
// let input: Vec<(Pos, char)> = input
let input: Vec<_> = input.lines()
let input: Vec<_> = input
.lines()
.enumerate()
.flat_map(|(y, l)| {
l.chars().enumerate().filter_map(move |(x, c)| {

View File

@@ -4,7 +4,6 @@ use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct Pos {
x: u8,
@@ -18,16 +17,28 @@ impl Pos {
fn neighbors(&self, max: &Pos) -> Vec<Pos> {
let mut res = Vec::new();
if self.x > 0 {
res.push(Pos { x: self.x - 1, ..self.clone() });
res.push(Pos {
x: self.x - 1,
..self.clone()
});
}
if self.y > 0 {
res.push(Pos { y: self.y - 1, ..self.clone() });
res.push(Pos {
y: self.y - 1,
..self.clone()
});
}
if self.x < max.x {
res.push(Pos { x: self.x + 1, ..self.clone() });
res.push(Pos {
x: self.x + 1,
..self.clone()
});
}
if self.y < max.y {
res.push(Pos { y: self.y + 1, ..self.clone() });
res.push(Pos {
y: self.y + 1,
..self.clone()
});
}
res
}
@@ -40,15 +51,16 @@ fn parse_input(input: &str) -> HashMap<Pos, usize> {
input
.lines()
.enumerate()
.filter_map(|(n, l)|
.filter_map(|(n, l)| {
if l.is_empty() {
None
} else {
let mut v: Vec<_> = l.split(",").map(|n| n.parse::<u8>().unwrap()).collect();
if v.len() < 2 {
return None
return None;
}
Some((Pos::init(v.remove(0), v.remove(0)), n))
}
})
.collect()
}
@@ -135,15 +147,15 @@ fn print_map(size: &Pos, walls: &HashMap<Pos, usize>, path: &Vec<Pos>) {
pub fn part_one(input: &str) -> Option<usize> {
let mut coords = parse_input(input);
let (limit, end) = if coords.len() < 100 { // Test case
let (limit, end) = if coords.len() < 100 {
// Test case
(12, Pos::init(6, 6))
} else {
(1024, Pos::init(70, 70))
};
coords.retain(|_, v| *v < limit);
bfs(&coords, Pos::init(0, 0), &end)
.map(|r| r.len())
bfs(&coords, Pos::init(0, 0), &end).map(|r| r.len())
}
pub fn part_two(input: &str) -> Option<String> {