|
| 1 | +use std::collections::{HashMap, HashSet, VecDeque}; |
| 2 | + |
| 3 | +advent_of_code::solution!(7); |
| 4 | + |
| 5 | +pub fn part_one(input: &str) -> Option<u64> { |
| 6 | + let grid = parse_grid(input); |
| 7 | + let start = find_start(&grid)?; |
| 8 | + let result = simulate_beams(&grid, start); |
| 9 | + Some(result) |
| 10 | +} |
| 11 | + |
| 12 | +pub fn part_two(input: &str) -> Option<u64> { |
| 13 | + let grid = parse_grid(input); |
| 14 | + let start = find_start(&grid)?; |
| 15 | + |
| 16 | + let mut memo = HashMap::new(); |
| 17 | + let result = count_paths(&grid, start, &mut memo); |
| 18 | + |
| 19 | + Some(result) |
| 20 | +} |
| 21 | + |
| 22 | +fn parse_grid(input: &str) -> Vec<Vec<char>> { |
| 23 | + input.lines().map(|line| line.chars().collect()).collect() |
| 24 | +} |
| 25 | + |
| 26 | +fn find_start(grid: &[Vec<char>]) -> Option<(usize, usize)> { |
| 27 | + for (row, line) in grid.iter().enumerate() { |
| 28 | + for (col, &ch) in line.iter().enumerate() { |
| 29 | + if ch == 'S' { |
| 30 | + return Some((row, col)); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + None |
| 35 | +} |
| 36 | + |
| 37 | +fn simulate_beams(grid: &[Vec<char>], start: (usize, usize)) -> u64 { |
| 38 | + let mut count = 0; |
| 39 | + let mut active = VecDeque::new(); |
| 40 | + let mut visited = HashSet::new(); |
| 41 | + |
| 42 | + active.push_back((start.0, start.1)); |
| 43 | + |
| 44 | + while let Some(beam_pos) = active.pop_front() { |
| 45 | + if let Some(splitter_pos) = find_next(grid, beam_pos) |
| 46 | + && visited.insert(splitter_pos) |
| 47 | + { |
| 48 | + count += 1; |
| 49 | + add_split_beams(grid, splitter_pos, &mut active); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + count |
| 54 | +} |
| 55 | + |
| 56 | +fn find_next(grid: &[Vec<char>], start: (usize, usize)) -> Option<(usize, usize)> { |
| 57 | + let (mut row, col) = start; |
| 58 | + |
| 59 | + loop { |
| 60 | + row += 1; |
| 61 | + |
| 62 | + if row >= grid.len() || col >= grid[row].len() { |
| 63 | + return None; |
| 64 | + } |
| 65 | + |
| 66 | + if grid[row][col] == '^' { |
| 67 | + return Some((row, col)); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +fn add_split_beams( |
| 73 | + grid: &[Vec<char>], |
| 74 | + splitter: (usize, usize), |
| 75 | + beams: &mut VecDeque<(usize, usize)>, |
| 76 | +) { |
| 77 | + let (row, col) = splitter; |
| 78 | + |
| 79 | + if col > 0 { |
| 80 | + beams.push_back((row, col - 1)); |
| 81 | + } |
| 82 | + |
| 83 | + if col < grid[row].len() - 1 { |
| 84 | + beams.push_back((row, col + 1)); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +fn count_paths( |
| 89 | + grid: &[Vec<char>], |
| 90 | + pos: (usize, usize), |
| 91 | + memo: &mut std::collections::HashMap<(usize, usize), u64>, |
| 92 | +) -> u64 { |
| 93 | + if let Some(&cached) = memo.get(&pos) { |
| 94 | + return cached; |
| 95 | + } |
| 96 | + |
| 97 | + let result = if let Some(splitter) = find_next(grid, pos) { |
| 98 | + let (row, col) = splitter; |
| 99 | + let mut total = 0; |
| 100 | + |
| 101 | + // Count paths going left |
| 102 | + if col > 0 { |
| 103 | + total += count_paths(grid, (row, col - 1), memo); |
| 104 | + } |
| 105 | + |
| 106 | + // Count paths going right |
| 107 | + if col < grid[row].len() - 1 { |
| 108 | + total += count_paths(grid, (row, col + 1), memo); |
| 109 | + } |
| 110 | + |
| 111 | + total |
| 112 | + } else { |
| 113 | + // Exited the grid - this counts as 1 timeline |
| 114 | + 1 |
| 115 | + }; |
| 116 | + |
| 117 | + memo.insert(pos, result); |
| 118 | + result |
| 119 | +} |
| 120 | + |
| 121 | +#[cfg(test)] |
| 122 | +mod tests { |
| 123 | + use super::*; |
| 124 | + |
| 125 | + #[test] |
| 126 | + fn test_part_one() { |
| 127 | + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); |
| 128 | + assert_eq!(result, Some(21)); |
| 129 | + } |
| 130 | + |
| 131 | + #[test] |
| 132 | + fn test_part_two() { |
| 133 | + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); |
| 134 | + assert_eq!(result, Some(40)); |
| 135 | + } |
| 136 | +} |
0 commit comments