From edc405e62e59d0684b738af806577908ec83c459 Mon Sep 17 00:00:00 2001 From: Russell Champoux Date: Wed, 11 Dec 2024 20:21:36 +0000 Subject: [PATCH] day 10 pt 1 --- data/examples/10-2.txt | 8 +++++ data/examples/10.txt | 4 +++ src/bin/10.rs | 81 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 data/examples/10-2.txt create mode 100644 data/examples/10.txt create mode 100644 src/bin/10.rs diff --git a/data/examples/10-2.txt b/data/examples/10-2.txt new file mode 100644 index 0000000..cada9b3 --- /dev/null +++ b/data/examples/10-2.txt @@ -0,0 +1,8 @@ +89010123 +78121874 +87430965 +96549874 +45678903 +32019012 +01329801 +10456732 diff --git a/data/examples/10.txt b/data/examples/10.txt new file mode 100644 index 0000000..a305b9d --- /dev/null +++ b/data/examples/10.txt @@ -0,0 +1,4 @@ +0123 +1234 +8765 +9876 diff --git a/src/bin/10.rs b/src/bin/10.rs new file mode 100644 index 0000000..5424afe --- /dev/null +++ b/src/bin/10.rs @@ -0,0 +1,81 @@ +use itertools::Itertools; + +advent_of_code::solution!(10); + +#[derive(Debug)] +#[derive(PartialEq)] +struct Path { + row: isize, + col: isize, + elevation: u32, +} + +pub fn part_one(input: &str) -> Option { + // build the grid + let grid = input.lines().map(|line| + line.chars().map(|c| c.to_digit(10).unwrap() ).collect_vec() + ).collect_vec(); + + let peaks = grid.iter().enumerate().flat_map(|(row_idx, row)| + row.iter().enumerate().filter_map(move |(col_idx, c)| + if *c == 9 { Some(Path { row: row_idx as isize, col: col_idx as isize, elevation: 9 }) } else { None } + ) + ).collect_vec(); + + // find the trailheads + let trailheads = grid.iter().enumerate().flat_map(|(row_idx, row)| + row.iter().enumerate().filter_map(move |(col_idx, c)| + if *c == 0 { Some(Path { row: row_idx as isize, col: col_idx as isize, elevation: 0 }) } else { None } + ) + ).collect_vec(); + + + let total_score = trailheads.iter().cartesian_product(&peaks).filter_map(|(trail, peak)| { + if can_navigate(&grid, trail, peak) { Some(true) } else { None } + }).count(); + + Some(total_score as u32) +} + +pub fn part_two(input: &str) -> Option { + None +} + +const DIRECTIONS: &[(isize, isize); 4] = &[(-1,0), (1,0), (0,-1), (0,1)]; // UP, DOWN, LEFT, RIGHT + +fn can_navigate(grid: &Vec>, start: &Path, end: &Path) -> bool { + let grid_height = grid.len(); + let grid_width = grid[0].len(); + let is_valid = |p: &Path| (0..grid_height as isize).contains(&p.row) && (0..grid_width as isize).contains(&p.col) && grid[p.row as usize][p.col as usize] == p.elevation; + + if start == end { return true } + + DIRECTIONS.iter().any(|d| { + let next_step = Path { row: &start.row + d.0, col: &start.col + d.1, elevation: start.elevation + 1 }; + // println!("{:?} ==> {:?} ?", start, end); + is_valid(&next_step) && can_navigate(&grid, &next_step, &end) + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_part_one() { + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(1)); + } + + #[test] + fn test_part_one_2() { + let result = part_one(&advent_of_code::template::read_file_part("examples", DAY, 2)); + assert_eq!(result, Some(36)); + } + + #[test] + fn test_part_two() { + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, None); + } +}