mirror of
https://github.com/mx42/aoc2024.rs.git
synced 2026-01-14 05:49:53 +01:00
chore: fmt + clippy
This commit is contained in:
@@ -11,7 +11,7 @@ enum Dir {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Dir {
|
impl Dir {
|
||||||
fn to_vec(&self) -> (i8, i8) {
|
fn to_vec(self) -> (i8, i8) {
|
||||||
match self {
|
match self {
|
||||||
Dir::Up => (0, -1),
|
Dir::Up => (0, -1),
|
||||||
Dir::Right => (1, 0),
|
Dir::Right => (1, 0),
|
||||||
@@ -44,11 +44,11 @@ impl Pos {
|
|||||||
fn move_towards(&self, v: (i8, i8), max: &Self) -> Option<Self> {
|
fn move_towards(&self, v: (i8, i8), max: &Self) -> Option<Self> {
|
||||||
let new_x: isize = self.x as isize + v.0 as isize;
|
let new_x: isize = self.x as isize + v.0 as isize;
|
||||||
let new_y: isize = self.y as isize + v.1 as isize;
|
let new_y: isize = self.y as isize + v.1 as isize;
|
||||||
if new_x >= 0
|
if new_x >= 0 && (new_x as usize) < max.x && new_y >= 0 && (new_y as usize) < max.y {
|
||||||
&& (new_x as usize) < max.x
|
Some(Pos {
|
||||||
&& new_y >= 0
|
x: new_x as usize,
|
||||||
&& (new_y as usize) < max.y {
|
y: new_y as usize,
|
||||||
Some(Pos { x: new_x as usize, y: new_y as usize })
|
})
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@@ -96,7 +96,8 @@ impl std::fmt::Debug for State {
|
|||||||
}
|
}
|
||||||
// screw the cpu
|
// screw the cpu
|
||||||
let match_walk = self
|
let match_walk = self
|
||||||
.walked.clone()
|
.walked
|
||||||
|
.clone()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|g| g.pos == pos)
|
.filter(|g| g.pos == pos)
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
@@ -110,40 +111,36 @@ impl std::fmt::Debug for State {
|
|||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
fn step(self) -> Option<Self> {
|
fn step(self) -> Option<Self> {
|
||||||
if self.is_last_step() {
|
if self.is_last_step() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let guard_walk_direction = self.guard.facing.to_vec();
|
let guard_walk_direction = self.guard.facing.to_vec();
|
||||||
let guard_walk = successors(
|
let guard_walk = successors(Some(self.guard.pos), |pos| {
|
||||||
Some(self.guard.pos),
|
|
||||||
|pos| {
|
|
||||||
if let Some(new_pos) = pos.move_towards(guard_walk_direction, &self.map_size) {
|
if let Some(new_pos) = pos.move_towards(guard_walk_direction, &self.map_size) {
|
||||||
if !self.walls.contains(&new_pos) {
|
if !self.walls.contains(&new_pos) {
|
||||||
return Some(new_pos)
|
return Some(new_pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
}
|
})
|
||||||
)
|
|
||||||
.collect::<Vec<Pos>>();
|
.collect::<Vec<Pos>>();
|
||||||
let guard_new_pos = guard_walk.last().unwrap().clone();
|
let guard_new_pos = guard_walk.last().unwrap().clone();
|
||||||
let mut walked: Vec<Guard> = self.walked.clone();
|
let mut walked: Vec<Guard> = self.walked.clone();
|
||||||
walked.extend(
|
walked.extend(guard_walk.iter().map(|p| Guard {
|
||||||
guard_walk.iter().map(|p| Guard { pos: p.clone(), facing: self.guard.facing })
|
pos: p.clone(),
|
||||||
);
|
facing: self.guard.facing,
|
||||||
|
}));
|
||||||
|
|
||||||
Some(Self {
|
Some(Self {
|
||||||
guard: Guard {
|
guard: Guard {
|
||||||
pos: guard_new_pos,
|
pos: guard_new_pos,
|
||||||
facing: self.guard.facing.rotate90(),
|
facing: self.guard.facing.rotate90(),
|
||||||
},
|
},
|
||||||
walked: walked,
|
walked,
|
||||||
..self
|
..self
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -154,8 +151,7 @@ impl State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn parse_line(line: &str, line_nb: usize) -> (Vec<Pos>, Option<Pos>) {
|
fn parse_line(line: &str, line_nb: usize) -> (Vec<Pos>, Option<Pos>) {
|
||||||
line
|
line.chars()
|
||||||
.chars()
|
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.fold(
|
.fold(
|
||||||
(Vec::<Pos>::new(), None),
|
(Vec::<Pos>::new(), None),
|
||||||
@@ -163,21 +159,16 @@ fn parse_line(line: &str, line_nb: usize) -> (Vec<Pos>, Option<Pos>) {
|
|||||||
'#' => {
|
'#' => {
|
||||||
wpos.push(Pos::init(pos, line_nb));
|
wpos.push(Pos::init(pos, line_nb));
|
||||||
(wpos, gpos)
|
(wpos, gpos)
|
||||||
},
|
|
||||||
'^' => {
|
|
||||||
(wpos, Some(Pos::init(pos, line_nb)))
|
|
||||||
},
|
|
||||||
_ => (wpos, gpos)
|
|
||||||
}
|
}
|
||||||
|
'^' => (wpos, Some(Pos::init(pos, line_nb))),
|
||||||
|
_ => (wpos, gpos),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_input(input: &str) -> State {
|
fn parse_input(input: &str) -> State {
|
||||||
let input = input.lines().collect::<Vec<_>>();
|
let input = input.lines().collect::<Vec<_>>();
|
||||||
let size = Pos::init(
|
let size = Pos::init(input[0].len(), input.len());
|
||||||
input[0].len(),
|
|
||||||
input.len()
|
|
||||||
);
|
|
||||||
let data = input
|
let data = input
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
@@ -190,14 +181,14 @@ fn parse_input(input: &str) -> State {
|
|||||||
Some(_) => (gl_wpos, gpos),
|
Some(_) => (gl_wpos, gpos),
|
||||||
_ => (gl_wpos, gl_gpos),
|
_ => (gl_wpos, gl_gpos),
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
if data.1.is_none() {
|
if data.1.is_none() {
|
||||||
panic!("Guard position not found?!");
|
panic!("Guard position not found?!");
|
||||||
}
|
}
|
||||||
let guard: Guard = Guard {
|
let guard: Guard = Guard {
|
||||||
pos: data.1.unwrap(),
|
pos: data.1.unwrap(),
|
||||||
facing: Dir::Up
|
facing: Dir::Up,
|
||||||
};
|
};
|
||||||
State {
|
State {
|
||||||
walls: data.0,
|
walls: data.0,
|
||||||
@@ -209,10 +200,9 @@ fn parse_input(input: &str) -> State {
|
|||||||
|
|
||||||
pub fn part_one(input: &str) -> Option<usize> {
|
pub fn part_one(input: &str) -> Option<usize> {
|
||||||
let state = parse_input(input);
|
let state = parse_input(input);
|
||||||
let last_state = successors(
|
let last_state = successors(Some(state), |s| s.clone().step())
|
||||||
Some(state),
|
.last()
|
||||||
|s| s.clone().step())
|
.unwrap();
|
||||||
.last().unwrap();
|
|
||||||
// println!("{:?}", last_state);
|
// println!("{:?}", last_state);
|
||||||
let mut visited = last_state
|
let mut visited = last_state
|
||||||
.walked
|
.walked
|
||||||
@@ -225,8 +215,8 @@ pub fn part_one(input: &str) -> Option<usize> {
|
|||||||
Some(visited.len())
|
Some(visited.len())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part_two(input: &str) -> Option<u32> {
|
pub fn part_two(_input: &str) -> Option<usize> {
|
||||||
let state = parse_input(input);
|
// let state = parse_input(input);
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user