advent_of_code_2022/aoc_08b/src/main.rs

71 lines
1.8 KiB
Rust

use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
fn main() {
let file_path = String::from("../aoc_08a/input");
let mut map = Vec::<Vec<u32>>::new();
let mut scene = Vec::<Vec<usize>>::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());
scene.push(Vec::new());
for c in ip.chars() {
map[row].push(c.to_digit(10).unwrap());
scene[row].push(0);
}
}
}
}
let mut max = 0;
for x in 1..map[0].len() - 1 {
for y in 1..map.len() - 1 {
let mut cur = 1;
let mut mul = 1;
while x + mul < map[0].len() - 1 && map[x][y] > map[x + mul][y] {
mul += 1;
}
cur *= mul;
mul = 1;
while x - mul > 0 && map[x][y] > map[x - mul][y] {
mul += 1;
}
cur *= mul;
mul = 1;
while y + mul < map.len() - 1 && map[x][y] > map[x][y + mul] {
mul += 1;
}
cur *= mul;
mul = 1;
while y - mul > 0 && map[x][y] > map[x][y - mul] {
mul += 1;
}
cur *= mul;
if cur > max {
max = cur;
}
scene[x][y] = cur;
}
}
println!("{:?}", scene);
println!("{:?}", max);
}
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())
}