This commit is contained in:
pjht 2022-12-08 08:45:04 -06:00
parent 557b27a427
commit 6ba07944da
3 changed files with 117 additions and 0 deletions

View File

@ -9,3 +9,4 @@ edition = "2021"
aoc-runner = "0.3.0" aoc-runner = "0.3.0"
aoc-runner-derive = "0.3.0" aoc-runner-derive = "0.3.0"
id_tree = "1.8.0" id_tree = "1.8.0"
simple-grid = "2.1.1"

115
src/day08.rs Normal file
View File

@ -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<isize> {
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<isize>) -> 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<isize>) -> 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
}

View File

@ -7,5 +7,6 @@ mod day04;
mod day05; mod day05;
mod day06; mod day06;
mod day07; mod day07;
mod day08;
aoc_lib! { year = 2022 } aoc_lib! { year = 2022 }