Skip to content

Commit

Permalink
Simplified day 6 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
3rfaan committed Dec 8, 2024
1 parent 8f4263c commit 6e1c923
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/bin/06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ pub fn part_one(input: &str) -> Option<u32> {

pub fn part_two(input: &str) -> Option<u32> {
let mut lab = Lab::from(input);

let origin = lab.guard;
let visited = lab.walk();

Some(
visited
lab.walk()
.iter()
.filter(|&&obstacle| lab.looping(origin, obstacle))
.filter(|&&obstacle| lab.is_looping(origin, obstacle))
.count() as u32,
)
}
Expand Down Expand Up @@ -44,10 +42,9 @@ impl Lab {
let mut visited = HashSet::new();

loop {
let next = self.guard.pos + self.guard.dir.offset();

visited.insert(self.guard.pos);

let next = self.guard.pos + self.guard.dir.offset();
match self.get(next) {
Some(b'#') => self.guard.dir = self.guard.dir.turn(),
Some(_) => self.guard.pos = next,
Expand All @@ -57,7 +54,7 @@ impl Lab {
visited
}

fn looping(&mut self, origin: Guard, obstacle: Pos) -> bool {
fn is_looping(&mut self, origin: Guard, obstacle: Pos) -> bool {
let mut visited = HashSet::new();

self.guard = origin;
Expand All @@ -68,7 +65,6 @@ impl Lab {
break true;
}
let next = self.guard.pos + self.guard.dir.offset();

match self.get(next) {
Some(b'#' | b'O') => self.guard.dir = self.guard.dir.turn(),
Some(_) => self.guard.pos = next,
Expand Down

0 comments on commit 6e1c923

Please sign in to comment.