Day 10 part 1

This commit is contained in:
Daniel Flanagan 2024-12-10 09:40:23 -06:00
parent 772478e4fb
commit b17ad64e2f

78
2024/rust/src/day10.rs Normal file
View file

@ -0,0 +1,78 @@
mod prelude;
pub use crate::prelude::*;
fn main() {
let input = day_input(10);
show_answers(part1(&input), part2(&input));
}
fn part1(input: &str) -> usize {
println!("{input}");
let mut sum = 0;
let map: Vec<Vec<u8>> = input
.lines()
.map(|l| l.bytes().map(|b| b - b'0').collect())
.collect();
for (y, row) in map.iter().enumerate() {
for (x, height) in row.iter().enumerate() {
let mut scores = HashSet::new();
if *height == 0 {
find_paths_to_nines(&map, *height, x, y, &mut scores);
sum += scores.len();
}
println!("{x} {y} {height}");
}
}
sum
}
fn find_paths_to_nines(
map: &Vec<Vec<u8>>,
height: u8,
x: usize,
y: usize,
scores: &mut HashSet<(usize, usize)>,
) {
if height == 9 {
scores.insert((x, y));
println!("found a 9");
}
for (nx, ny) in [
(x + 1, y),
(x.saturating_sub(1), y),
(x, y + 1),
(x, y.saturating_sub(1)),
] {
if nx >= map[0].len() || ny >= map.len() {
continue;
}
let next_height = map[ny][nx];
if next_height.saturating_sub(height) == 1 {
println!("{height} -> {next_height} at {nx},{ny}");
find_paths_to_nines(map, next_height, nx, ny, scores)
}
}
}
fn part2(input: &str) -> usize {
0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let input = r#"89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732"#;
assert_eq!(part1(input), 36);
assert_eq!(part2(input), 0);
}
}