Skip to content

Commit aef34db

Browse files
committed
2025 07.2
Victory for "almost exactly solving part 2 in part 1 because you didn't read it properly" once again
1 parent 7590aa6 commit aef34db

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/aoc/y2025/day07.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// You should have received a copy of the Licence along with this work. If not, see:
77
// <https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12>.
88
// 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};
1010
use std::fmt::Display;
1111

1212
use aoc_macros::VoidState;
@@ -91,12 +91,48 @@ pub fn part_1(data: crate::DataIn) -> crate::AoCResult<String> {
9191
Ok(ret.to_string())
9292
}
9393

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+
94127
inventory::submit!(crate::AoCDay {
95128
year: "2025",
96129
day: "7",
97130
part_1: crate::AoCPart {
98131
main: part_1,
99132
example: part_1
100133
},
101-
part_2: None
134+
part_2: Some(crate::AoCPart {
135+
main: part_2,
136+
example: part_2
137+
})
102138
});

0 commit comments

Comments
 (0)