Clean up graph searching code.
This commit is contained in:
parent
4322bcc5c4
commit
0a0a4ec5d9
1 changed files with 13 additions and 25 deletions
|
@ -8,41 +8,28 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pt1(grid: &Grid) -> usize {
|
fn pt1(grid: &Grid) -> usize {
|
||||||
dfs(grid)
|
score(grid)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pt2(grid: &Grid) -> usize {
|
fn pt2(grid: &Grid) -> usize {
|
||||||
bfs(grid)
|
rank(grid)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dfs(grid: &Grid) -> usize {
|
fn score(grid: &Grid) -> usize {
|
||||||
let mut total = 0;
|
let mut total = 0;
|
||||||
|
|
||||||
for head in grid.heads.iter() {
|
for head in grid.heads.iter() {
|
||||||
let mut htotal = 0;
|
let mut htotal = 0;
|
||||||
let mut processed = HashSet::new();
|
|
||||||
let mut q = Vec::new();
|
let mut q = Vec::new();
|
||||||
|
let mut nines = HashSet::new();
|
||||||
q.push(*head);
|
q.push(*head);
|
||||||
while let Some(current) = q.pop() {
|
while let Some(current) = q.pop() {
|
||||||
if !processed.contains(¤t) {
|
if grid.get(¤t).unwrap() == 9 && nines.insert(current) {
|
||||||
q.push(current);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut do_top = true;
|
|
||||||
let nexts = grid.next_step(¤t);
|
|
||||||
for next in nexts.iter() {
|
|
||||||
if !processed.contains(next) {
|
|
||||||
q.push(*next);
|
|
||||||
do_top = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if do_top {
|
|
||||||
let _ = q.pop();
|
|
||||||
if processed.insert(current) && grid.get(¤t).unwrap() == 9 {
|
|
||||||
htotal += 1;
|
htotal += 1;
|
||||||
}
|
}
|
||||||
|
let nexts = grid.next_step(¤t);
|
||||||
|
for next in nexts.iter() {
|
||||||
|
q.push(*next);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
total += htotal;
|
total += htotal;
|
||||||
|
@ -51,7 +38,7 @@ fn dfs(grid: &Grid) -> usize {
|
||||||
total
|
total
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bfs(grid: &Grid) -> usize {
|
fn rank(grid: &Grid) -> usize {
|
||||||
let mut total = 0;
|
let mut total = 0;
|
||||||
|
|
||||||
for head in grid.heads.iter() {
|
for head in grid.heads.iter() {
|
||||||
|
@ -59,12 +46,11 @@ fn bfs(grid: &Grid) -> usize {
|
||||||
let mut q = VecDeque::new();
|
let mut q = VecDeque::new();
|
||||||
q.push_back(*head);
|
q.push_back(*head);
|
||||||
while let Some(current) = q.pop_front() {
|
while let Some(current) = q.pop_front() {
|
||||||
let nexts = grid.next_step(¤t);
|
if grid.get(¤t).unwrap() == 9 {
|
||||||
for next in nexts.iter() {
|
|
||||||
if grid.get(next).unwrap() == 9 {
|
|
||||||
htotal += 1;
|
htotal += 1;
|
||||||
}
|
}
|
||||||
|
let nexts = grid.next_step(¤t);
|
||||||
|
for next in nexts.iter() {
|
||||||
q.push_back(*next);
|
q.push_back(*next);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -157,6 +143,8 @@ impl Grid {
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// returns list of neighbor nodes in a dag where the edges are all one step in
|
||||||
|
// distance
|
||||||
fn next_step(&self, loc: &Loc) -> Vec<Loc> {
|
fn next_step(&self, loc: &Loc) -> Vec<Loc> {
|
||||||
let alt = self.rows[loc.row][loc.col];
|
let alt = self.rows[loc.row][loc.col];
|
||||||
let target = alt + 1;
|
let target = alt + 1;
|
||||||
|
|
Loading…
Reference in a new issue