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) -> 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, 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, 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, row_idx: usize, col_idx: usize) -> u32 {} fn scenic_score_vec(data: &Matrix, 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 { Matrix::new( data.lines() .map(|line| { line.chars() .map(|c| c.to_digit(10).unwrap()) .collect::>() }) .collect::>>(), ) } #[cfg(test)] mod test { use super::*; const TEST_DATA: &str = "30373 25512 65332 33549 35390"; fn with_input(test: F) where F: Fn(Matrix) -> () + 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)); } }