Day 8. So much easier than 7 whewwww
This commit is contained in:
parent
cf3867f355
commit
273561c79f
5 changed files with 175 additions and 0 deletions
56
day8/src/main.rs
Normal file
56
day8/src/main.rs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
fn main() {
|
||||
let contents = std::fs::read_to_string("data.txt").expect("Failed to read file");
|
||||
let grid = contents // make it a grid of u32
|
||||
.lines()
|
||||
.map(|l| {
|
||||
l.chars()
|
||||
.map(|c| c.to_digit(10).unwrap())
|
||||
.collect::<Vec<u32>>()
|
||||
})
|
||||
.collect::<Vec<Vec<u32>>>();
|
||||
|
||||
let height = grid.len();
|
||||
|
||||
let mut visible = 0;
|
||||
let mut high_score = 0;
|
||||
|
||||
for (row, line) in grid.iter().enumerate() {
|
||||
for (col, tree) in line.iter().enumerate() {
|
||||
let width = line.len();
|
||||
|
||||
let north = (0..row).map(|r| grid[r][col]).rev().collect::<Vec<u32>>();
|
||||
let south = (row + 1..height)
|
||||
.map(|r| grid[r][col])
|
||||
.collect::<Vec<u32>>();
|
||||
let west = (0..col).map(|c| grid[row][c]).rev().collect::<Vec<u32>>();
|
||||
let east = (col + 1..width).map(|c| grid[row][c]).collect::<Vec<u32>>();
|
||||
|
||||
let dirs = vec![north, south, west, east];
|
||||
|
||||
// Part 1
|
||||
if row == 0 || col == 0 || row == height - 1 || col == width - 1 {
|
||||
visible += 1; // if it is on the edge, it is visible
|
||||
} else {
|
||||
if dirs.iter().any(|dir| dir.iter().all(|t| tree > t)) {
|
||||
visible += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Part 2
|
||||
let score = dirs.iter().fold(1, |acc, dir| {
|
||||
let position = dir.into_iter().position(|x| tree <= x);
|
||||
acc * match position {
|
||||
Some(x) => x + 1,
|
||||
None => dir.len(), // hit an edge. return the distance to the edge
|
||||
}
|
||||
});
|
||||
|
||||
if score > high_score {
|
||||
high_score = score;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("Number Visible: {:?}", visible);
|
||||
println!("Highest Score: {:?}", high_score);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue