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