day6, part2
This commit is contained in:
parent
3718169c39
commit
53816fed73
1 changed files with 29 additions and 2 deletions
|
@ -29,13 +29,30 @@ fn pt2(input: &str) -> usize {
|
|||
let guard = board.guard;
|
||||
|
||||
let no = [guard.0, guard.1.next(guard.0)];
|
||||
let mut trace = HashSet::new();
|
||||
while let Some((loc, _)) = board.step() {
|
||||
if !no.contains(&loc) {
|
||||
trace.insert(loc);
|
||||
}
|
||||
}
|
||||
|
||||
while let Some(loc) = board.step() {}
|
||||
for loc in trace {
|
||||
board = Board::new(input);
|
||||
let mut steps = HashSet::new();
|
||||
board.set(loc, Cell::Obstacle);
|
||||
steps.insert(board.guard);
|
||||
while let Some(guard) = board.step() {
|
||||
if !steps.insert(guard) {
|
||||
total += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
total
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
|
||||
enum Dir {
|
||||
#[default]
|
||||
N,
|
||||
|
@ -127,6 +144,16 @@ impl Board {
|
|||
parse(input).unwrap()
|
||||
}
|
||||
|
||||
fn set(&mut self, loc: Loc, cell: Cell) -> bool {
|
||||
if let Some(row) = self.cells.get_mut(loc.0) {
|
||||
if let Some(c) = row.get_mut(loc.1) {
|
||||
*c = cell;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn step(&mut self) -> Option<(Loc, Dir)> {
|
||||
let (loc, dir) = &mut self.guard;
|
||||
*self
|
||||
|
|
Loading…
Reference in a new issue