Compare commits
No commits in common. "0a0a4ec5d90c6d6928433c80fa61b89be655d1ab" and "81f7933cc0e6f1db78142d6b99c1d29af2a8fbd7" have entirely different histories.
0a0a4ec5d9
...
81f7933cc0
1 changed files with 21 additions and 44 deletions
|
@ -1,60 +1,45 @@
|
||||||
use std::collections::{HashSet, VecDeque};
|
use std::collections::HashSet;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let input = std::fs::read_to_string("input").unwrap();
|
let input = std::fs::read_to_string("input").unwrap();
|
||||||
let grid = Grid::new(&input);
|
let grid = Grid::new(&input);
|
||||||
println!("{}", pt1(&grid));
|
println!("{}", pt1(&grid));
|
||||||
println!("{}", pt2(&grid));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pt1(grid: &Grid) -> usize {
|
fn pt1(grid: &Grid) -> usize {
|
||||||
score(grid)
|
dfs(grid)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pt2(grid: &Grid) -> usize {
|
fn dfs(grid: &Grid) -> usize {
|
||||||
rank(grid)
|
|
||||||
}
|
|
||||||
|
|
||||||
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 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 grid.get(¤t).unwrap() == 9 && nines.insert(current) {
|
if !processed.contains(¤t) {
|
||||||
htotal += 1;
|
q.push(current);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut do_top = true;
|
||||||
let nexts = grid.next_step(¤t);
|
let nexts = grid.next_step(¤t);
|
||||||
for next in nexts.iter() {
|
for next in nexts.iter() {
|
||||||
q.push(*next);
|
if !processed.contains(next) {
|
||||||
|
q.push(*next);
|
||||||
|
do_top = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if do_top {
|
||||||
|
let _ = q.pop();
|
||||||
|
processed.insert(current);
|
||||||
|
if grid.get(¤t).unwrap() == 9 {
|
||||||
|
total += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
total += htotal;
|
|
||||||
}
|
|
||||||
|
|
||||||
total
|
|
||||||
}
|
|
||||||
|
|
||||||
fn rank(grid: &Grid) -> usize {
|
|
||||||
let mut total = 0;
|
|
||||||
|
|
||||||
for head in grid.heads.iter() {
|
|
||||||
let mut htotal = 0;
|
|
||||||
let mut q = VecDeque::new();
|
|
||||||
q.push_back(*head);
|
|
||||||
while let Some(current) = q.pop_front() {
|
|
||||||
if grid.get(¤t).unwrap() == 9 {
|
|
||||||
htotal += 1;
|
|
||||||
}
|
|
||||||
let nexts = grid.next_step(¤t);
|
|
||||||
for next in nexts.iter() {
|
|
||||||
q.push_back(*next);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
total += htotal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
total
|
total
|
||||||
|
@ -143,8 +128,6 @@ 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;
|
||||||
|
@ -186,10 +169,4 @@ mod test {
|
||||||
let g = Grid::new(INPUT);
|
let g = Grid::new(INPUT);
|
||||||
assert_eq!(36, pt1(&g));
|
assert_eq!(36, pt1(&g));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn p2() {
|
|
||||||
let g = Grid::new(INPUT);
|
|
||||||
assert_eq!(81, pt2(&g));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue