68 lines
1.6 KiB
Rust
68 lines
1.6 KiB
Rust
#![feature(iterator_try_collect, absolute_path, hash_set_entry)]
|
|
|
|
use std::fs;
|
|
use std::io::{self, BufRead};
|
|
|
|
use grid::Grid;
|
|
|
|
struct Tree {
|
|
height: u8,
|
|
visible: bool,
|
|
}
|
|
|
|
impl Tree {
|
|
fn new(height: u8) -> Self {
|
|
Tree {
|
|
height,
|
|
visible: false,
|
|
}
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let filename = "etc/p08.txt";
|
|
let file = fs::File::open(filename).expect("Can't open file");
|
|
let mut count: Option<usize> = None;
|
|
let vec: Vec<Tree> = io::BufReader::new(file)
|
|
.lines()
|
|
.flatten()
|
|
.flat_map(|l| {
|
|
count.get_or_insert_with(|| l.chars().count());
|
|
l.chars()
|
|
.map(|c| Tree::new(c.to_digit(10).unwrap() as u8))
|
|
.collect::<Vec<Tree>>()
|
|
})
|
|
.collect();
|
|
|
|
let mut grid = Grid::from_vec(vec, count.unwrap());
|
|
|
|
fn height_pass(acc: u8, t: &mut Tree) -> u8 {
|
|
if t.height > acc {
|
|
t.visible = true;
|
|
}
|
|
t.height.max(acc)
|
|
}
|
|
|
|
grid.iter_col_mut(0).for_each(|t| t.visible = true);
|
|
grid.iter_col_mut(grid.cols() - 1)
|
|
.for_each(|t| t.visible = true);
|
|
grid.iter_row_mut(0).for_each(|t| t.visible = true);
|
|
grid.iter_row_mut(grid.rows() - 1)
|
|
.for_each(|t| t.visible = true);
|
|
|
|
for i in 0..grid.cols() {
|
|
grid.iter_col_mut(i).fold(0, height_pass);
|
|
grid.iter_col_mut(i).rev().fold(0, height_pass);
|
|
}
|
|
for i in 0..grid.rows() {
|
|
grid.iter_row_mut(i).fold(0, height_pass);
|
|
grid.iter_row_mut(i).rev().fold(0, height_pass);
|
|
}
|
|
|
|
dbg!(grid.iter().filter(|t| t.visible).count());
|
|
}
|
|
|
|
fn part1() {}
|
|
|
|
fn part2() {}
|