80 lines
2.3 KiB
Rust
80 lines
2.3 KiB
Rust
use std::fs::File;
|
|
use std::io::{self, BufRead};
|
|
use std::path::Path;
|
|
use std::collections::HashMap;
|
|
|
|
fn main() {
|
|
let file_path = String::from("input");
|
|
let mut visible_trees = HashMap::<(usize, usize), usize>::new();
|
|
let mut map = Vec::<Vec<u32>>::new();
|
|
|
|
println!("In file {}", file_path);
|
|
|
|
if let Ok(lines) = read_lines(file_path) {
|
|
// Consumes the iterator, returns an (Optional) String
|
|
for (row, line) in lines.enumerate() {
|
|
if let Ok(ip) = line {
|
|
map.push(Vec::new());
|
|
for c in ip.chars() {
|
|
map[row].push(c.to_digit(10).unwrap());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for y in 0..map.len() {
|
|
let mut max = map[y][0];
|
|
*visible_trees.entry((0, y)).or_insert(0) += 1;
|
|
for x in 1..map[y].len() {
|
|
println!("lr {}", x);
|
|
if map[y][x] > max {
|
|
*visible_trees.entry((x, y)).or_insert(0) += 1;
|
|
max = map[y][x];
|
|
}
|
|
}
|
|
|
|
max = *map[y].last().unwrap();
|
|
*visible_trees.entry((map[y].len() - 1, y)).or_insert(0) += 1;
|
|
for x in (0..map[y].len() - 1).rev() {
|
|
println!("rl {}", x);
|
|
if map[y][x] > max {
|
|
*visible_trees.entry((x, y)).or_insert(0) += 1;
|
|
max = map[y][x];
|
|
}
|
|
}
|
|
}
|
|
|
|
for x in 0..map[0].len() {
|
|
let mut max = map[0][x];
|
|
*visible_trees.entry((x, 0)).or_insert(0) += 1;
|
|
for y in 1..map.len() {
|
|
println!("ud {}", y);
|
|
if map[y][x] > max {
|
|
*visible_trees.entry((x, y)).or_insert(0) += 1;
|
|
max = map[y][x];
|
|
}
|
|
}
|
|
|
|
max = map.last().unwrap()[x];
|
|
*visible_trees.entry((x, map.len() - 1)).or_insert(0) += 1;
|
|
for y in (0..map.len() - 1).rev() {
|
|
println!("du {}", y);
|
|
if map[y][x] > max {
|
|
*visible_trees.entry((x, y)).or_insert(0) += 1;
|
|
max = map[y][x];
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
println!("{:?}", map);
|
|
println!("{:?}", visible_trees);
|
|
println!("{}", visible_trees.len());
|
|
}
|
|
|
|
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
|
where P: AsRef<Path>, {
|
|
let file = File::open(filename)?;
|
|
Ok(io::BufReader::new(file).lines())
|
|
}
|