|
6 | 6 | // You should have received a copy of the Licence along with this work. If not, see: |
7 | 7 | // <https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12>. |
8 | 8 | // See the Licence for the specific language governing permissions and limitations under the Licence. |
9 | | -use std::collections::HashSet; |
| 9 | +use std::collections::{HashMap, HashSet}; |
10 | 10 | use std::fmt::Display; |
11 | 11 |
|
12 | 12 | use aoc_macros::VoidState; |
@@ -91,12 +91,48 @@ pub fn part_1(data: crate::DataIn) -> crate::AoCResult<String> { |
91 | 91 | Ok(ret.to_string()) |
92 | 92 | } |
93 | 93 |
|
| 94 | +pub fn part_2(data: crate::DataIn) -> crate::AoCResult<String> { |
| 95 | + let grid = SparseGrid::<GridState>::new_from_chars( |
| 96 | + data |
| 97 | + // get rid of the empty lines since they don't do anything |
| 98 | + .filter(|line| !line.chars().all(|c| c == '.')), |
| 99 | + )?; |
| 100 | + let max = grid.max_key(); |
| 101 | + |
| 102 | + let ret: u64 = (1..=max.y) |
| 103 | + .fold( |
| 104 | + grid.iter() |
| 105 | + .filter(|(_, state)| matches!(state, GridState::Start)) |
| 106 | + .map(|(coord, _)| (*coord, 1)) |
| 107 | + .collect(), |
| 108 | + |beams: HashMap<Coord2D, u64>, y| { |
| 109 | + let mut new_beams = HashMap::with_capacity(beams.len()); |
| 110 | + for (mut coord, count) in beams.into_iter() { |
| 111 | + coord.y = y; |
| 112 | + if matches!(grid.get(&coord), Some(GridState::Splitter)) { |
| 113 | + *new_beams.entry(coord + Direction::East.into()).or_default() += count; |
| 114 | + *new_beams.entry(coord + Direction::West.into()).or_default() += count; |
| 115 | + } else { |
| 116 | + *new_beams.entry(coord).or_default() += count; |
| 117 | + } |
| 118 | + } |
| 119 | + new_beams |
| 120 | + }, |
| 121 | + ) |
| 122 | + .values() |
| 123 | + .sum(); |
| 124 | + Ok(ret.to_string()) |
| 125 | +} |
| 126 | + |
94 | 127 | inventory::submit!(crate::AoCDay { |
95 | 128 | year: "2025", |
96 | 129 | day: "7", |
97 | 130 | part_1: crate::AoCPart { |
98 | 131 | main: part_1, |
99 | 132 | example: part_1 |
100 | 133 | }, |
101 | | - part_2: None |
| 134 | + part_2: Some(crate::AoCPart { |
| 135 | + main: part_2, |
| 136 | + example: part_2 |
| 137 | + }) |
102 | 138 | }); |
0 commit comments