diff --git a/Cargo.toml b/Cargo.toml index 412739c..12d0cb2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ edition = "2021" aoc-runner = "0.3.0" aoc-runner-derive = "0.3.0" id_tree = "1.8.0" +simple-grid = "2.1.1" diff --git a/src/day08.rs b/src/day08.rs new file mode 100644 index 0000000..623fe32 --- /dev/null +++ b/src/day08.rs @@ -0,0 +1,115 @@ +use aoc_runner_derive::{aoc, aoc_generator}; +use simple_grid::{Grid, GridIndex}; + +#[aoc_generator(day8)] +fn input_generator(input: &str) -> Grid { + let mut grid = Grid::new_default(0, 0); + for row in input.lines().map(|line| { + line.chars() + .map(|c| c.to_string().parse().unwrap()) + .collect() + }) { + grid.push_row(row); + } + grid +} + +#[aoc(day8, part1)] +fn solve_part1(input: &Grid) -> usize { + let mut vis_grid = Grid::new_default(input.width(), input.height()); + for row in 1..input.height() - 1 { + let mut max_height = *input.get(GridIndex::new(0, row)).unwrap(); + for column in 1..input.width() - 1 { + let idx = GridIndex::new(column, row); + let height = *input.get(idx).unwrap(); + if height > max_height { + *vis_grid.get_mut(idx).unwrap() = true; + max_height = height; + } + } + } + for row in 1..input.height() - 1 { + let mut max_height = *input.get(GridIndex::new(input.width() - 1, row)).unwrap(); + for column in (1..input.width() - 1).rev() { + let idx = GridIndex::new(column, row); + let height = *input.get(idx).unwrap(); + if height > max_height { + *vis_grid.get_mut(idx).unwrap() = true; + max_height = height; + } + } + } + for column in 1..input.width() - 1 { + let mut max_height = *input.get(GridIndex::new(column, 0)).unwrap(); + for row in 1..input.height() - 1 { + let idx = GridIndex::new(column, row); + let height = *input.get(idx).unwrap(); + if height > max_height { + *vis_grid.get_mut(idx).unwrap() = true; + max_height = height; + } + } + } + for column in 1..input.width() - 1 { + let mut max_height = *input + .get(GridIndex::new(column, input.height() - 1)) + .unwrap(); + for row in (1..input.height() - 1).rev() { + let idx = GridIndex::new(column, row); + let height = *input.get(idx).unwrap(); + if height > max_height { + *vis_grid.get_mut(idx).unwrap() = true; + max_height = height; + } + } + } + vis_grid.cell_iter().filter(|&&item| item).count() + + dbg!((input.width() * 2) + ((input.height() - 2) * 2)) +} + +#[aoc(day8, part2)] +fn solve_part2(input: &Grid) -> usize { + let mut max_score = 0; + for row in input.rows() { + for column in input.columns() { + let idx = GridIndex::new(column, row); + let height = *input.get(idx).unwrap(); + let mut down_count = ((row + 1)..input.height()) // Look down + .map(|row| GridIndex::new(column, row)) + .take_while(|&idx| *input.get(idx).unwrap() < height) + .count(); + if down_count < input.height() - (row + 1) { + down_count += 1; + } + let mut up_count = (0..row) // Look up + .rev() + .map(|row| GridIndex::new(column, row)) + .take_while(|&idx| *input.get(idx).unwrap() < height) + .count(); + if up_count < row { + up_count += 1; + } + let mut right_count = ((column + 1)..input.width()) // Look right + .map(|column| GridIndex::new(column, row)) + .take_while(|&idx| *input.get(idx).unwrap() < height) + .count(); + if right_count < input.width() - (column + 1) { + right_count += 1; + } + let mut left_count = (0..column) // Look left + .rev() + .map(|column| GridIndex::new(column, row)) + .take_while(|&idx| *input.get(idx).unwrap() < height) + .count(); + if left_count < column { + left_count += 1; + } + let score = down_count * up_count * right_count * left_count; + + if score > max_score { + max_score = score; + } + } + } + max_score +} diff --git a/src/lib.rs b/src/lib.rs index 76d945b..990c058 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,5 +7,6 @@ mod day04; mod day05; mod day06; mod day07; +mod day08; aoc_lib! { year = 2022 }