Compare commits

...

2 Commits

Author SHA1 Message Date
9a64352fc3 Day 8, puzzle 2 2022-12-14 16:25:10 +01:00
03a8692162 Day 8, puzzle 1 2022-12-14 16:17:38 +01:00
4 changed files with 125 additions and 0 deletions

10
src/bin/d8p1.rs Normal file
View File

@ -0,0 +1,10 @@
use aoc22::{
day8::{self, find_visible},
util,
};
pub fn main() {
let grid = day8::parse_grid(&util::parse_input());
println!("There are {} visible trees", find_visible(&grid).len());
}

22
src/bin/d8p2.rs Normal file
View File

@ -0,0 +1,22 @@
use aoc22::{
day8::{self, calc_scenic_score},
util,
};
pub fn main() {
let grid = day8::parse_grid(&util::parse_input());
let rows = grid.len();
assert!(rows > 0);
let cols = grid.get(0).unwrap().len();
assert!(cols > 0);
let max_score = (0..rows)
.map(move |r| (0..cols).map(move |c| (r, c)))
.flatten()
.map(|c| calc_scenic_score(&grid, c))
.max()
.unwrap();
println!("Max scenic score is {}", max_score);
}

92
src/day8.rs Normal file
View File

@ -0,0 +1,92 @@
use std::collections::HashSet;
pub type Grid = Vec<Vec<u32>>;
pub fn parse_grid(input: &String) -> Grid {
let mut result = Vec::new();
for line in input.lines() {
result.push(line.chars().map(|c| c.to_digit(10).unwrap()).collect());
}
let row_lens: Vec<usize> = result.iter().map(Vec::len).collect();
assert_eq!(row_lens.iter().max(), row_lens.iter().min());
result
}
pub type Coordinate = (usize, usize);
pub fn find_visible(grid: &Grid) -> HashSet<Coordinate> {
let mut result = HashSet::new();
let rows = grid.len();
assert!(rows > 0);
let cols = grid.get(0).unwrap().len();
assert!(cols > 0);
for row in 0..rows {
let mut left_indices = (0..cols).map(|c| (row, c));
let mut right_indices = left_indices.clone().rev();
result.extend(check_indices(&grid, &mut left_indices));
result.extend(check_indices(&grid, &mut right_indices));
}
for col in 0..cols {
let mut top_indices = (0..rows).map(|r| (r, col));
let mut bottom_indices = top_indices.clone().rev();
result.extend(check_indices(&grid, &mut top_indices));
result.extend(check_indices(&grid, &mut bottom_indices));
}
result
}
fn check_indices(grid: &Grid, coords: &mut dyn Iterator<Item = Coordinate>) -> Vec<Coordinate> {
let mut result = Vec::new();
let mut max_height: i64 = -1;
for (row, col) in coords {
let height = *grid.get(row).unwrap().get(col).unwrap() as i64;
if height > max_height {
result.push((row, col));
max_height = height;
}
}
result
}
pub fn calc_scenic_score(grid: &Grid, (row, col): Coordinate) -> usize {
let rows = grid.len();
assert!(rows > 0);
let cols = grid.get(0).unwrap().len();
assert!(cols > 0);
let height = *grid.get(row).unwrap().get(col).unwrap();
let mut left = (0..col).map(|c| (row, c)).rev();
let mut right = (col + 1..cols).map(|c| (row, c));
let mut up = (0..row).map(|r| (r, col)).rev();
let mut down = (row + 1..rows).map(|r| (r, col));
let mut directions: Vec<&mut dyn Iterator<Item = Coordinate>> =
vec![&mut left, &mut right, &mut up, &mut down];
directions
.iter_mut()
.map(|d| calc_viewing_distance(grid, height, *d))
.product()
}
fn calc_viewing_distance(
grid: &Grid,
start_height: u32,
coords: &mut dyn Iterator<Item = Coordinate>,
) -> usize {
let mut result = 0;
for (row, col) in coords {
result += 1;
let height = *grid.get(row).unwrap().get(col).unwrap();
if height >= start_height {
break;
}
}
result
}

View File

@ -5,4 +5,5 @@ pub mod day4;
pub mod day5; pub mod day5;
pub mod day6; pub mod day6;
pub mod day7; pub mod day7;
pub mod day8;
pub mod util; pub mod util;