-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-8.rs
130 lines (112 loc) · 3.62 KB
/
day-8.rs
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
use std::fs;
use std::collections::HashSet;
fn score(map : &Vec<Vec<u8>>, tree_x : usize, tree_y : usize) -> u32 {
let map_height = map.len();
let map_width = map[0].len();
let mut scenic_score : u32 = 1;
let mut seen : u32 = 0;
for x in tree_x + 1..map_height {
seen += 1;
if (map[x][tree_y]) >= map[tree_x][tree_y] {
break; // count the tree above, but no more in this direction
}
}
scenic_score *= seen;
let mut seen : u32 = 0;
for x in (0..tree_x).rev() {
seen += 1;
if (map[x][tree_y]) >= map[tree_x][tree_y] {
break; // count the tree above, but no more in this direction
}
}
scenic_score *= seen;
let mut seen : u32 = 0;
for y in tree_y + 1..map_width {
seen += 1;
if (map[tree_x][y]) >= map[tree_x][tree_y] {
break; // count the tree above, but no more in this direction
}
}
scenic_score *= seen;
let mut seen : u32 = 0;
for y in (0..tree_y).rev() {
seen += 1;
if (map[tree_x][y]) >= map[tree_x][tree_y] {
break; // count the tree above, but no more in this direction
}
}
scenic_score *= seen;
return scenic_score
}
fn main() {
let file_str = fs::read_to_string("day-8.input").expect("Failed to read file");
let mut map : Vec<Vec<u8>> = Vec::new();
for line in file_str.trim().split('\n') {
let mut row = Vec::new();
for height in line.bytes() {
row.push(height);
}
map.push(row);
}
let mut wins : HashSet<(u32, u32)> = HashSet::new();
let map_width = map[0].len();
map.iter().enumerate().fold(
(0..map_width).map(|_| b'0' - 1).collect(),
|maxes : Vec<u8>, (x, row)| {
row.iter().zip(maxes).enumerate().map(|(y, (&height, prev_max))| {
if height > prev_max {
wins.insert((x as u32, y as u32));
return height;
}
return prev_max;
}).collect()
}
);
map.iter().enumerate().rev().fold(
(0..map_width).map(|_| b'0' - 1).collect(),
|maxes : Vec<u8>, (x, row)| {
row.iter().zip(maxes).enumerate().map(|(y, (&height, prev_max))| {
if height > prev_max {
wins.insert((x as u32, y as u32));
return height;
}
return prev_max;
}).collect()
}
);
map.iter().enumerate().map(
|(x, row)| {
row.iter().enumerate().fold(b'0' - 1, |prev_max, (y, &height)| {
if height > prev_max {
wins.insert((x as u32, y as u32));
return height;
}
return prev_max;
})
}
).for_each(drop);
map.iter().enumerate().map(
|(x, row)| {
row.iter().enumerate().rev().fold(b'0' - 1, |prev_max, (y, &height)| {
if height > prev_max {
wins.insert((x as u32, y as u32));
return height;
}
return prev_max;
})
}
).for_each(drop);
println!("total visible from outside: {}", wins.len());
let max_scenic_score = (
map.iter().enumerate().map(
|(x, row)| {
row.iter().enumerate().map(
|(y, _)| {
score(&map, x, y)
}
).max()
}
).max()
).unwrap().unwrap();
println!("max_scenic_score: {}", max_scenic_score);
}