Part 2 WIP, broken and messy
This commit is contained in:
parent
c607fb7a93
commit
81a992ff65
|
@ -35,7 +35,13 @@ fn main() {
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let mut grid = Grid::from_vec(vec, count.unwrap());
|
let mut grid = Grid::from_vec(vec, count.unwrap());
|
||||||
|
println!("PART 1 ---------------");
|
||||||
|
part1(&mut grid);
|
||||||
|
println!("\nPART 2 ---------------");
|
||||||
|
part2(&mut grid);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part1(grid: &mut Grid<Tree>) {
|
||||||
fn height_pass(acc: u8, t: &mut Tree) -> u8 {
|
fn height_pass(acc: u8, t: &mut Tree) -> u8 {
|
||||||
if t.height > acc {
|
if t.height > acc {
|
||||||
t.visible = true;
|
t.visible = true;
|
||||||
|
@ -43,12 +49,10 @@ fn main() {
|
||||||
t.height.max(acc)
|
t.height.max(acc)
|
||||||
}
|
}
|
||||||
|
|
||||||
grid.iter_col_mut(0)
|
grid.iter_col_mut(0).for_each(|t| t.visible = true);
|
||||||
.for_each(|t| t.visible = true);
|
|
||||||
grid.iter_col_mut(grid.cols() - 1)
|
grid.iter_col_mut(grid.cols() - 1)
|
||||||
.for_each(|t| t.visible = true);
|
.for_each(|t| t.visible = true);
|
||||||
grid.iter_row_mut(0)
|
grid.iter_row_mut(0).for_each(|t| t.visible = true);
|
||||||
.for_each(|t| t.visible = true);
|
|
||||||
grid.iter_row_mut(grid.rows() - 1)
|
grid.iter_row_mut(grid.rows() - 1)
|
||||||
.for_each(|t| t.visible = true);
|
.for_each(|t| t.visible = true);
|
||||||
|
|
||||||
|
@ -64,6 +68,86 @@ fn main() {
|
||||||
dbg!(grid.iter().filter(|t| t.visible).count());
|
dbg!(grid.iter().filter(|t| t.visible).count());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part1() {}
|
fn scenic_score(grid: &Grid<Tree>, (row, col): (usize, usize)) -> u32 {
|
||||||
|
fn count_visible((i, max): (u32, u32), tree: &Tree) -> (u32, u32) {
|
||||||
|
if tree.height as u32 >= max {
|
||||||
|
(i, tree.height as u32)
|
||||||
|
} else {
|
||||||
|
(i + 1, max)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn part2() {}
|
let (rows, cols) = grid.size();
|
||||||
|
|
||||||
|
let (a, _) = if row > 0 {
|
||||||
|
(0..row - 1)
|
||||||
|
.rev()
|
||||||
|
.map(|r| grid.get(r, col).unwrap())
|
||||||
|
.fold((1, 0), count_visible)
|
||||||
|
} else {
|
||||||
|
(0, 0)
|
||||||
|
};
|
||||||
|
let (b, _) = if row < rows - 1 {
|
||||||
|
(row + 1..rows)
|
||||||
|
.map(|r| grid.get(r, col).unwrap())
|
||||||
|
.fold((1, 0), count_visible)
|
||||||
|
} else {
|
||||||
|
(0, 0)
|
||||||
|
};
|
||||||
|
let (c, _) = if col > 0 {
|
||||||
|
(0..col - 1)
|
||||||
|
.rev()
|
||||||
|
.map(|c| grid.get(row, c).unwrap())
|
||||||
|
.fold((1, 0), count_visible)
|
||||||
|
} else {
|
||||||
|
(0, 0)
|
||||||
|
};
|
||||||
|
let (d, _) = if col < cols - 1 {
|
||||||
|
(col + 1..cols)
|
||||||
|
.map(|c| grid.get(row, c).unwrap())
|
||||||
|
.fold((1, 0), count_visible)
|
||||||
|
} else {
|
||||||
|
(0, 0)
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("{},{}: {}, {}, {}, {}", row, col, a, b, c, d);
|
||||||
|
|
||||||
|
a * b * c * d
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part2(grid: &Grid<Tree>) {
|
||||||
|
let cols = grid.cols();
|
||||||
|
dbg!(grid
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, _)| scenic_score(grid, (i / cols, i % cols)))
|
||||||
|
.max());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use grid::grid;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn small_grid() {
|
||||||
|
let grid = grid![
|
||||||
|
[3, 0, 3, 7, 3]
|
||||||
|
[2, 5, 5, 1, 2]
|
||||||
|
[6, 5, 3, 3, 2]
|
||||||
|
[3, 3, 5, 4, 9]
|
||||||
|
[3, 5, 3, 9, 0]
|
||||||
|
];
|
||||||
|
let vec: Vec<Tree> = grid.iter().map(|n| Tree::new(*n)).collect();
|
||||||
|
let grid = Grid::from_vec(vec, 5);
|
||||||
|
|
||||||
|
let scores: Vec<u32> = grid
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, _)| scenic_score(&grid, (i / 5, i % 5)))
|
||||||
|
.collect();
|
||||||
|
dbg!(&scores);
|
||||||
|
|
||||||
|
assert_eq!(8, *scores.iter().max().unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue