|
| 1 | +use heapless::FnvIndexSet; |
| 2 | +use mygrid::{direction::ORTHOGONAL, grid::Grid, point::Point}; |
| 3 | +use std::collections::VecDeque; |
| 4 | + |
| 5 | +type Found = FnvIndexSet<Point, 128>; |
| 6 | + |
| 7 | +advent_of_code::solution!(10); |
| 8 | + |
| 9 | +pub fn part_one(input: &str) -> Option<u32> { |
| 10 | + let grid: Grid<u8> = Grid::new_from_str(input, |c| c.to_digit(10).unwrap() as u8); |
| 11 | + |
| 12 | + let trailheads = grid |
| 13 | + // TODO: par_iter_item_and_position() |
| 14 | + .iter_item_and_position() |
| 15 | + .filter(|(_, &value)| value == 0) |
| 16 | + .map(|(pos, _)| pos); |
| 17 | + |
| 18 | + let trailhead_scores = trailheads |
| 19 | + .map(|starting_point| { |
| 20 | + let mut visited = Grid::new(grid.width, grid.height, false); |
| 21 | + let mut found = Found::new(); |
| 22 | + |
| 23 | + let mut q = VecDeque::with_capacity(128); |
| 24 | + q.push_back(starting_point); |
| 25 | + visited[starting_point] = true; |
| 26 | + |
| 27 | + while let Some(current) = q.pop_front() { |
| 28 | + visited[current] = true; |
| 29 | + if grid[current] == 9 { |
| 30 | + visited[current] = true; |
| 31 | + found.insert(current).unwrap(); |
| 32 | + continue; |
| 33 | + } |
| 34 | + for n in ORTHOGONAL |
| 35 | + .iter() |
| 36 | + .map(|&dir| current + dir) |
| 37 | + .filter(|&p| grid.is_in_bounds(p)) |
| 38 | + .filter(|&p| grid[p] == grid[current] + 1) |
| 39 | + .filter(|&p| !visited[p]) |
| 40 | + { |
| 41 | + q.push_front(n); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + found.len() as u32 |
| 46 | + }) |
| 47 | + .sum(); |
| 48 | + |
| 49 | + Some(trailhead_scores) |
| 50 | +} |
| 51 | + |
| 52 | +pub fn part_two(input: &str) -> Option<u32> { |
| 53 | + let grid: Grid<u8> = Grid::new_from_str(input, |c| c.to_digit(10).unwrap() as u8); |
| 54 | + |
| 55 | + let trailheads = grid |
| 56 | + // TODO: par_iter_item_and_position() |
| 57 | + .iter_item_and_position() |
| 58 | + .filter(|(_, &value)| value == 0) |
| 59 | + .map(|(pos, _)| pos); |
| 60 | + |
| 61 | + let trailhead_scores = trailheads |
| 62 | + .map(|starting_point| { |
| 63 | + let mut found = 0; |
| 64 | + |
| 65 | + let mut q = VecDeque::with_capacity(128); |
| 66 | + q.push_back(starting_point); |
| 67 | + |
| 68 | + while let Some(current) = q.pop_front() { |
| 69 | + if grid[current] == 9 { |
| 70 | + found += 1; |
| 71 | + continue; |
| 72 | + } |
| 73 | + for n in ORTHOGONAL |
| 74 | + .iter() |
| 75 | + .map(|&dir| current + dir) |
| 76 | + .filter(|&p| grid.is_in_bounds(p)) |
| 77 | + .filter(|&p| grid[p] == grid[current] + 1) |
| 78 | + { |
| 79 | + q.push_front(n); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + found |
| 84 | + }) |
| 85 | + .sum(); |
| 86 | + |
| 87 | + Some(trailhead_scores) |
| 88 | +} |
| 89 | + |
| 90 | +#[cfg(test)] |
| 91 | +mod tests { |
| 92 | + use super::*; |
| 93 | + |
| 94 | + #[test] |
| 95 | + fn test_part_one_1() { |
| 96 | + let input = "0123\n1234\n8765\n9876\n"; |
| 97 | + let result = part_one(&input); |
| 98 | + assert_eq!(result, Some(1)); |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn test_part_one_2() { |
| 103 | + let input = "8880888\n8881888\n8882888\n6543456\n7888887\n8888888\n9888889\n"; |
| 104 | + let result = part_one(&input); |
| 105 | + assert_eq!(result, Some(2)); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn test_part_one_3() { |
| 110 | + let input = "8890889\n8881898\n8882887\n6543456\n7658987\n8768888\n9878888\n"; |
| 111 | + let result = part_one(&input); |
| 112 | + assert_eq!(result, Some(4)); |
| 113 | + } |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn test_part_one_4() { |
| 117 | + let input = "1088988\n2888888\n3888788\n4567654\n8888883\n8889882\n8888801\n"; |
| 118 | + let result = part_one(&input); |
| 119 | + assert_eq!(result, Some(3)); |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn test_part_one_5() { |
| 124 | + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); |
| 125 | + assert_eq!(result, Some(36)); |
| 126 | + } |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn test_part_two() { |
| 130 | + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); |
| 131 | + assert_eq!(result, Some(81)); |
| 132 | + } |
| 133 | +} |
0 commit comments