You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.8 KiB
Rust

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);
}