day 10 part 2
This commit is contained in:
30
src/day10.rs
30
src/day10.rs
@@ -17,9 +17,11 @@ pub fn day_main() {
|
|||||||
type RiddleResult = usize;
|
type RiddleResult = usize;
|
||||||
|
|
||||||
fn part1(input: &str) -> RiddleResult {
|
fn part1(input: &str) -> RiddleResult {
|
||||||
|
solve(input, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve(input: &str, visit_once: bool) -> usize {
|
||||||
let grid = Grid::parse(input);
|
let grid = Grid::parse(input);
|
||||||
// println!("{grid:?}");
|
|
||||||
// println!("----");
|
|
||||||
let m: HashMap<(i64, i64), u32> = HashMap::from_iter(grid.entries().map(|((x, y), c)| {
|
let m: HashMap<(i64, i64), u32> = HashMap::from_iter(grid.entries().map(|((x, y), c)| {
|
||||||
(
|
(
|
||||||
(x, y),
|
(x, y),
|
||||||
@@ -31,7 +33,6 @@ fn part1(input: &str) -> RiddleResult {
|
|||||||
)
|
)
|
||||||
}));
|
}));
|
||||||
let grid = Grid::from(m);
|
let grid = Grid::from(m);
|
||||||
// println!("{grid:?}");
|
|
||||||
let trail_heads = grid.entries().filter(|(_, h)| **h == 0).collect_vec();
|
let trail_heads = grid.entries().filter(|(_, h)| **h == 0).collect_vec();
|
||||||
let mut result = 0;
|
let mut result = 0;
|
||||||
for (th, th_height) in trail_heads {
|
for (th, th_height) in trail_heads {
|
||||||
@@ -39,14 +40,13 @@ fn part1(input: &str) -> RiddleResult {
|
|||||||
let mut visited = HashSet::new();
|
let mut visited = HashSet::new();
|
||||||
let mut queue = vec![(th, th_height)];
|
let mut queue = vec![(th, th_height)];
|
||||||
while let Some((p, h)) = queue.pop() {
|
while let Some((p, h)) = queue.pop() {
|
||||||
if visited.contains(&(p, h)) {
|
if visit_once && visited.contains(&(p, h)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
visited.insert((p, h));
|
visited.insert((p, h));
|
||||||
|
|
||||||
if *h == 9 {
|
if *h == 9 {
|
||||||
count += 1;
|
count += 1;
|
||||||
// println!("found {p:?}");
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,19 +61,17 @@ fn part1(input: &str) -> RiddleResult {
|
|||||||
if let Some(nh) = grid.get(next) {
|
if let Some(nh) = grid.get(next) {
|
||||||
if *nh == h + 1 {
|
if *nh == h + 1 {
|
||||||
queue.push((next, nh));
|
queue.push((next, nh));
|
||||||
// println!("going from {:?} to {:?}", (p, h), (next, nh));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// println!("from {th:?} found {count}");
|
|
||||||
result += count;
|
result += count;
|
||||||
}
|
}
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part2(_input: &str) -> RiddleResult {
|
fn part2(input: &str) -> RiddleResult {
|
||||||
0
|
solve(input, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -126,6 +124,18 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test2() {
|
fn test2() {
|
||||||
assert_eq!(part2(TEST_INPUT), 0);
|
assert_eq!(
|
||||||
|
part2(
|
||||||
|
r".....0.
|
||||||
|
..4321.
|
||||||
|
..5..2.
|
||||||
|
..6543.
|
||||||
|
..7..4.
|
||||||
|
..8765.
|
||||||
|
..9....
|
||||||
|
"
|
||||||
|
),
|
||||||
|
3
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user