d8p2
This commit is contained in:
parent
343d09f460
commit
82f4503b3e
@ -4,55 +4,53 @@ use std::fs;
|
|||||||
fn main() {
|
fn main() {
|
||||||
let input = fs::read_to_string(&env::args().nth(1).unwrap()).unwrap();
|
let input = fs::read_to_string(&env::args().nth(1).unwrap()).unwrap();
|
||||||
let forest: Vec<Vec<i8>> = input.lines()
|
let forest: Vec<Vec<i8>> = input.lines()
|
||||||
.map(|line| line.chars().map(|c| c.to_digit(10).unwrap() as i8).collect())
|
.map(|line| line.chars()
|
||||||
|
.map(|c| c.to_digit(10).unwrap() as i8).collect())
|
||||||
.collect();
|
.collect();
|
||||||
let nrows = forest.len();
|
let nrows = forest.len();
|
||||||
let ncols = forest[0].len();
|
let ncols = forest[0].len();
|
||||||
|
|
||||||
let min_from_left: Vec<Vec<i8>> = forest.iter()
|
let mut scores = vec![vec![1; ncols]; nrows];
|
||||||
.map(|row| row.iter()
|
for r in 1..nrows-1 {
|
||||||
.scan(-1, |h, &tree| {
|
for c in 1..ncols-1 {
|
||||||
let old = *h; *h = i8::max(*h as i8, tree); Some(old)
|
let h = forest[r][c];
|
||||||
})
|
|
||||||
.collect())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let min_from_right: Vec<Vec<i8>> = forest.iter()
|
let mut j = c - 1;
|
||||||
.map(|row: &Vec<i8>| row.iter()
|
let mut count = 0;
|
||||||
.rev()
|
while j > 0 && forest[r][j] < h {
|
||||||
.scan(-1, |h, &tree| {
|
count += 1;
|
||||||
let old = *h; *h = i8::max(*h as i8, tree); Some(old)
|
j -= 1;
|
||||||
})
|
|
||||||
.collect::<Vec<i8>>().iter()
|
|
||||||
.rev().map(|x| *x)
|
|
||||||
.collect())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let mut min_from_top: Vec<Vec<i8>> = vec![vec![-1; ncols]; nrows];
|
|
||||||
for c in 0..ncols {
|
|
||||||
for r in 1..nrows {
|
|
||||||
min_from_top[r][c] = i8::max(min_from_top[r-1][c], forest[r-1][c]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut min_from_bot: Vec<Vec<i8>> = vec![vec![-1; ncols]; nrows];
|
|
||||||
for c in 0..ncols {
|
|
||||||
for r in (0..nrows-1).rev() {
|
|
||||||
min_from_bot[r][c] = i8::max(min_from_bot[r+1][c], forest[r+1][c]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut sum = 0;
|
|
||||||
for r in 0..nrows {
|
|
||||||
for c in 0..ncols {
|
|
||||||
if forest[r][c] > min_from_left[r][c]
|
|
||||||
|| forest[r][c] > min_from_right[r][c]
|
|
||||||
|| forest[r][c] > min_from_top[r][c]
|
|
||||||
|| forest[r][c] > min_from_bot[r][c] {
|
|
||||||
sum += 1;
|
|
||||||
}
|
}
|
||||||
|
scores[r][c] *= count + 1;
|
||||||
|
|
||||||
|
let mut j = c + 1;
|
||||||
|
let mut count = 0;
|
||||||
|
while j < ncols-1 && forest[r][j] < h {
|
||||||
|
count += 1;
|
||||||
|
j += 1;
|
||||||
|
}
|
||||||
|
scores[r][c] *= count + 1;
|
||||||
|
|
||||||
|
let mut i = r - 1;
|
||||||
|
let mut count = 0;
|
||||||
|
while i > 0 && forest[i][c] < h {
|
||||||
|
count += 1;
|
||||||
|
i -= 1;
|
||||||
|
}
|
||||||
|
scores[r][c] *= count + 1;
|
||||||
|
|
||||||
|
let mut i = r + 1;
|
||||||
|
let mut count = 0;
|
||||||
|
while i < nrows-1 && forest[i][c] < h {
|
||||||
|
count += 1;
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
scores[r][c] *= count + 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!("{:?}", sum);
|
|
||||||
|
let answer = scores.iter().map(|row| row.iter().max()).max();
|
||||||
|
println!("{:?}", answer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
59
day8/src/main1.rs
Normal file
59
day8/src/main1.rs
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
use std::env;
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let input = fs::read_to_string(&env::args().nth(1).unwrap()).unwrap();
|
||||||
|
let forest: Vec<Vec<i8>> = input.lines()
|
||||||
|
.map(|line| line.chars()
|
||||||
|
.map(|c| c.to_digit(10).unwrap() as i8).collect())
|
||||||
|
.collect();
|
||||||
|
let nrows = forest.len();
|
||||||
|
let ncols = forest[0].len();
|
||||||
|
|
||||||
|
let min_from_left: Vec<Vec<i8>> = forest.iter()
|
||||||
|
.map(|row| row.iter()
|
||||||
|
.scan(-1, |h, &tree| {
|
||||||
|
let old = *h; *h = i8::max(*h as i8, tree); Some(old)
|
||||||
|
})
|
||||||
|
.collect())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let min_from_right: Vec<Vec<i8>> = forest.iter()
|
||||||
|
.map(|row: &Vec<i8>| row.iter()
|
||||||
|
.rev()
|
||||||
|
.scan(-1, |h, &tree| {
|
||||||
|
let old = *h; *h = i8::max(*h as i8, tree); Some(old)
|
||||||
|
})
|
||||||
|
.collect::<Vec<i8>>().iter()
|
||||||
|
.rev().map(|x| *x)
|
||||||
|
.collect())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let mut min_from_top: Vec<Vec<i8>> = vec![vec![-1; ncols]; nrows];
|
||||||
|
for c in 0..ncols {
|
||||||
|
for r in 1..nrows {
|
||||||
|
min_from_top[r][c] = i8::max(min_from_top[r-1][c], forest[r-1][c]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut min_from_bot: Vec<Vec<i8>> = vec![vec![-1; ncols]; nrows];
|
||||||
|
for c in 0..ncols {
|
||||||
|
for r in (0..nrows-1).rev() {
|
||||||
|
min_from_bot[r][c] = i8::max(min_from_bot[r+1][c], forest[r+1][c]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut sum = 0;
|
||||||
|
for r in 0..nrows {
|
||||||
|
for c in 0..ncols {
|
||||||
|
if forest[r][c] > min_from_left[r][c]
|
||||||
|
|| forest[r][c] > min_from_right[r][c]
|
||||||
|
|| forest[r][c] > min_from_top[r][c]
|
||||||
|
|| forest[r][c] > min_from_bot[r][c] {
|
||||||
|
sum += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("{:?}", sum);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user