1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
use std::collections::HashMap;
static DAY: u8 = 8;
fn main() {
let input = advent::read_lines(DAY);
println!("{DAY}a: {}", visible_trees(&input));
println!("{DAY}b: {}", highest_scenic_score(&input));
}
#[derive(Default, Debug)]
struct NeighborVisibility {
up: usize,
down: usize,
left: usize,
right: usize,
}
fn visible_trees(input: &[String]) -> usize {
let mut map = Vec::new();
for line in input {
let heights = line.chars().map(|x| x.to_digit(10).unwrap() as usize).collect::<Vec<_>>();
map.push(heights);
}
let mut visibilities = HashMap::<_,NeighborVisibility>::new();
/* visibilities from top to bottom */
for x in 1 .. map[0].len() - 1 {
let mut current_vis = map[0][x];
#[allow(clippy::needless_range_loop)]
for y in 1 .. map.len() - 1 {
let vis_entry = visibilities.entry((x,y)).or_default();
vis_entry.up = current_vis;
current_vis = current_vis.max(map[y][x]);
}
}
/* visibilities from bottom to top */
for x in 1 .. map[0].len() - 1 {
let mut current_vis = map[map.len()-1][x];
for y in (1 .. map.len() - 1).rev() {
let vis_entry = visibilities.entry((x,y)).or_default();
vis_entry.down = current_vis;
current_vis = current_vis.max(map[y][x]);
}
}
/* visibilities from left to right */
for y in 1 .. map.len() - 1 {
let mut current_vis = map[y][0];
for x in 1 .. map[0].len() - 1 {
let vis_entry = visibilities.entry((x,y)).or_default();
vis_entry.left = current_vis;
current_vis = current_vis.max(map[y][x]);
}
}
/* visibilities from right to left */
for y in 1 .. map.len() - 1 {
let mut current_vis = map[y][map[0].len()-1];
for x in (1 .. map[0].len() - 1).rev() {
let vis_entry = visibilities.entry((x,y)).or_default();
vis_entry.right = current_vis;
current_vis = current_vis.max(map[y][x]);
}
}
let mut total_visible = 2 * (map.len() - 1) + 2 * (map[0].len() - 1);
for (pos, visibility) in visibilities {
let height = map[pos.1][pos.0];
if height > visibility.up || height > visibility.down || height > visibility.left || height > visibility.right {
total_visible += 1;
}
}
total_visible
}
fn highest_scenic_score(input: &[String]) -> usize {
let mut map = Vec::new();
for line in input {
let heights = line.chars().map(|x| x.to_digit(10).unwrap() as usize).collect::<Vec<_>>();
map.push(heights);
}
let mut visibilities = HashMap::<_,NeighborVisibility>::new();
let update_range_vector = |range: &mut [usize; 10], height: usize|{
/* all heights less than the specified one will start with a
new visibility of one; for greater heights the distance
increases by 1. */
for (i, val) in range.iter_mut().enumerate() {
if i <= height as usize {
*val = 1;
} else {
*val += 1;
}
}
};
/* visibilities from top to bottom */
for x in 0 .. map[0].len() {
let mut ranges = [0; 10];
#[allow(clippy::needless_range_loop)]
for y in 0 .. map.len() {
let height = map[y][x];
let vis_entry = visibilities.entry((x,y)).or_default();
vis_entry.up = ranges[height];
update_range_vector(&mut ranges, height);
}
}
/* visibilities from bottom to top */
for x in 0 .. map[0].len() {
let mut ranges = [0; 10];
for y in (0 .. map.len()).rev() {
let height = map[y][x];
let vis_entry = visibilities.entry((x,y)).or_default();
vis_entry.down = ranges[height];
update_range_vector(&mut ranges, height);
}
}
/* visibilities from left to right */
for y in 0 .. map.len() {
let mut ranges = [0; 10];
for x in 0 .. map[0].len() {
let height = map[y][x];
let vis_entry = visibilities.entry((x,y)).or_default();
vis_entry.left = ranges[height];
update_range_vector(&mut ranges, height);
}
}
/* visibilities from right to left */
for y in 0 .. map.len() {
let mut ranges = [0; 10];
for x in (0 .. map[0].len()).rev() {
let height = map[y][x];
let vis_entry = visibilities.entry((x,y)).or_default();
vis_entry.right = ranges[height];
update_range_vector(&mut ranges, height);
}
}
visibilities.values()
.map(|v| v.up * v.down * v.left * v.right)
.max()
.unwrap()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let input = [
"30373",
"25512",
"65332",
"33549",
"35390",
].iter().map(|&x| String::from(x)).collect::<Vec<_>>();
assert_eq!(visible_trees(&input), 21);
assert_eq!(highest_scenic_score(&input), 8);
}
}
|