add day 8
This commit is contained in:
87
src/day8/main.rs
Normal file
87
src/day8/main.rs
Normal file
@ -0,0 +1,87 @@
|
||||
use base::read_file;
|
||||
|
||||
struct Map {
|
||||
tiles: Vec<Vec<u32>>,
|
||||
width: usize,
|
||||
height: usize,
|
||||
}
|
||||
|
||||
impl Map {
|
||||
fn is_visible(&self, x: usize, y: usize) -> (bool, u32) {
|
||||
let height = self.tiles[x][y];
|
||||
|
||||
let mut visible_west = true;
|
||||
let mut visible_north = true;
|
||||
let mut visible_east = true;
|
||||
let mut visible_south = true;
|
||||
|
||||
let mut trees_visible_west = 0;
|
||||
for x in (0..x).rev() {
|
||||
trees_visible_west += 1;
|
||||
if self.tiles[x][y] >= height {
|
||||
visible_west = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let mut trees_visible_north = 0;
|
||||
for y in (0..y).rev() {
|
||||
trees_visible_north += 1;
|
||||
if self.tiles[x][y] >= height {
|
||||
visible_north = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let mut trees_visible_east = 0;
|
||||
for x in (x + 1)..self.width {
|
||||
trees_visible_east += 1;
|
||||
if self.tiles[x][y] >= height {
|
||||
visible_east = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let mut trees_visible_south = 0;
|
||||
for y in (y + 1)..self.height {
|
||||
trees_visible_south += 1;
|
||||
if self.tiles[x][y] >= height {
|
||||
visible_south = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
(visible_west || visible_north || visible_east || visible_south, trees_visible_south * trees_visible_east * trees_visible_north * trees_visible_west)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let lines = read_file("day8.txt");
|
||||
let mut map = vec![];
|
||||
for line in lines {
|
||||
map.push(line.chars().filter_map(|c| c.to_digit(10)).collect::<Vec<_>>());
|
||||
}
|
||||
|
||||
let map = Map {
|
||||
width: map.len(),
|
||||
height: map[0].len(),
|
||||
tiles: map,
|
||||
};
|
||||
|
||||
map.is_visible(1, 2);
|
||||
|
||||
let mut count = 0;
|
||||
let mut highest_score = 0;
|
||||
for x in 0..map.width {
|
||||
for y in 0..map.height {
|
||||
let (visible, score) = map.is_visible(x, y);
|
||||
if visible { count += 1 }
|
||||
if score > highest_score { highest_score = score }
|
||||
}
|
||||
}
|
||||
|
||||
println!("Tree that are visible: {count}");
|
||||
println!("Highest score was : {highest_score}");
|
||||
}
|
Reference in New Issue
Block a user