107 lines
2.8 KiB
Rust
107 lines
2.8 KiB
Rust
|
use crate::matrix::Matrix;
|
||
|
|
||
|
const INPUT: &str = include_str!("../data/day8.txt");
|
||
|
|
||
|
pub fn part1() -> String {
|
||
|
format!("{}", count_visible_trees(input(INPUT)))
|
||
|
}
|
||
|
|
||
|
pub fn part2() -> String {
|
||
|
unimplemented!()
|
||
|
}
|
||
|
|
||
|
fn count_visible_trees(data: Matrix<u32>) -> usize {
|
||
|
let mut cnt = 0;
|
||
|
for row_idx in 0..data.rows {
|
||
|
for col_idx in 0..data.cols {
|
||
|
if is_visible(&data, row_idx, col_idx) {
|
||
|
cnt = cnt + 1;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
cnt
|
||
|
}
|
||
|
|
||
|
fn is_visible(data: &Matrix<u32>, row_idx: usize, col_idx: usize) -> bool {
|
||
|
is_visible_vec(data.row(row_idx), col_idx) || is_visible_vec(data.col(col_idx), row_idx)
|
||
|
}
|
||
|
|
||
|
fn is_visible_vec(data: Vec<u32>, cell: usize) -> bool {
|
||
|
let (before, rest) = data.split_at(cell);
|
||
|
let (target, after) = rest.split_at(1);
|
||
|
let target = target[0];
|
||
|
let before = before.into_iter().all(|tree| *tree < target);
|
||
|
let after = after.into_iter().all(|tree| *tree < target);
|
||
|
before || after
|
||
|
}
|
||
|
|
||
|
fn scenic_score(data: &Matrix<u32>, row_idx: usize, col_idx: usize) -> u32 {}
|
||
|
|
||
|
fn scenic_score_vec(data: &Matrix<u32>, cell: usize) -> u32 {
|
||
|
let (before, rest) = data.split_at(cell);
|
||
|
let (target, after) = rest.split_at(1);
|
||
|
let target = target[0];
|
||
|
}
|
||
|
|
||
|
fn input(data: &str) -> Matrix<u32> {
|
||
|
Matrix::new(
|
||
|
data.lines()
|
||
|
.map(|line| {
|
||
|
line.chars()
|
||
|
.map(|c| c.to_digit(10).unwrap())
|
||
|
.collect::<Vec<u32>>()
|
||
|
})
|
||
|
.collect::<Vec<Vec<u32>>>(),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
#[cfg(test)]
|
||
|
mod test {
|
||
|
use super::*;
|
||
|
|
||
|
const TEST_DATA: &str = "30373
|
||
|
25512
|
||
|
65332
|
||
|
33549
|
||
|
35390";
|
||
|
|
||
|
fn with_input<F>(test: F)
|
||
|
where
|
||
|
F: Fn(Matrix<u32>) -> () + std::panic::UnwindSafe,
|
||
|
{
|
||
|
test(input(TEST_DATA));
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn it_tests_visibility() {
|
||
|
with_input(|forest| {
|
||
|
assert_eq!(is_visible(&forest, 0, 0), true);
|
||
|
assert_eq!(is_visible(&forest, 1, 2), true);
|
||
|
assert_eq!(is_visible(&forest, 1, 2), true);
|
||
|
assert_eq!(is_visible(&forest, 1, 3), false);
|
||
|
assert_eq!(is_visible(&forest, 2, 1), true);
|
||
|
assert_eq!(is_visible(&forest, 2, 2), false);
|
||
|
assert_eq!(is_visible(&forest, 2, 3), true);
|
||
|
assert_eq!(is_visible(&forest, 3, 1), false);
|
||
|
assert_eq!(is_visible(&forest, 3, 2), true);
|
||
|
assert_eq!(is_visible(&forest, 3, 3), false);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn it_counts_visible_trees() {
|
||
|
with_input(|forest| assert_eq!(count_visible_trees(forest), 21));
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn it_solves_part1() {
|
||
|
let forest = input(INPUT);
|
||
|
assert_eq!(count_visible_trees(forest), 1533);
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn it_counts_sightlines() {
|
||
|
with_input(|forest| assert_eq!(scenic_score(forest, 1, 2), 4));
|
||
|
}
|
||
|
}
|