-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
203 lines (180 loc) · 5.03 KB
/
main.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use std::fmt::{self, Display, Formatter};
pub fn main() {
let data = include_str!("input.txt");
let (map, size) = get_map(data);
let visible = look_in(&map, size);
println!("Part 1: {}", part_one(&visible));
println!("Part 2: {}", part_two(&map, &visible, size, 8));
}
fn part_one(visible: &Map<bool>) -> usize {
visible.iter()
.flatten()
.filter(|&b| *b)
.count()
}
fn part_two(map: &Map<u8>, visible: &Map<bool>, size: usize, lim: u8) -> u64 {
let mut max_score = 0_u64;
for row in 0..size {
for col in 0..size {
if visible[row][col] && map[row][col] > lim {
let score = scenic_score(map, row, col, size);
if score > max_score {
max_score = score;
}
}
}
}
max_score
}
type Map<T> = [[T; 99]; 99];
struct MapWrap<T>(Map<T>);
impl Display for MapWrap<bool> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
for row in self.0 {
for elem in row {
if elem {
write!(f, "#")?;
} else {
write!(f, ".")?;
}
}
writeln!(f)?;
}
Ok(())
}
}
fn get_map(data: &str) -> (Map<u8>, usize) {
let mut map: Map<u8> = [[0; 99]; 99];
let mut size: usize = 0;
for (row, line) in data.lines().enumerate() {
for (col, ascii_digit) in line.bytes().enumerate() {
// Add one so the zero hieght trees aren't zero any more
map[row][col] = ascii_digit - b'0' + 1;
}
size = row;
}
(map, size + 1)
}
fn scenic_score(map: &Map<u8>, row: usize, col: usize, size: usize) -> u64 {
let curr = map[row][col];
let (mut up, mut down, mut left, mut right) = (0_u64, 0_u64, 0_u64, 0_u64);
// Quit out on boundaries
if row == 0 || row == size - 1 || col == 0 || col == size - 1 {
return 0;
}
// Look up
for (dist, i) in (0..row).rev().enumerate() {
if i == 0 || map[i][col] >= curr {
up = (dist + 1).try_into().unwrap();
break;
}
}
// Look down
for (dist, i) in (row + 1..size).enumerate() {
if i == size - 1 || map[i][col] >= curr {
down = (dist + 1).try_into().unwrap();
break;
}
}
// Look left
for (dist, i) in (0..col).rev().enumerate() {
if i == 0 || map[row][i] >= curr {
left = (dist + 1).try_into().unwrap();
break;
}
}
// Look right
for (dist, i) in (col + 1..size).enumerate() {
if i == size - 1 || map[row][i] >= curr {
right = (dist + 1).try_into().unwrap();
break;
}
}
up * down * left * right
}
fn look_in(map: &Map<u8>, size: usize) -> Map<bool> {
let mut visible: Map<bool> = [[false; 99]; 99];
look_in_down(map, &mut visible, size);
look_in_left(map, &mut visible, size);
look_in_up(map, &mut visible, size);
look_in_right(map, &mut visible, size);
visible
}
fn look_in_down(map: &Map<u8>, visible: &mut Map<bool>, size: usize) {
let mut max;
for col in 0..size {
max = 0_u8;
for row in 0..size {
if map[row][col] > max {
max = map[row][col];
visible[row][col] = true;
if max == 10 {
break;
}
}
}
}
}
fn look_in_left(map: &Map<u8>, visible: &mut Map<bool>, size: usize) {
let mut max;
for row in 0..size {
max = 0_u8;
for col in (0..size).rev() {
if map[row][col] > max {
max = map[row][col];
visible[row][col] = true;
if max == 10 {
break;
}
}
}
}
}
fn look_in_up(map: &Map<u8>, visible: &mut Map<bool>, size: usize) {
let mut max;
for col in 0..size {
max = 0_u8;
for row in (0..size).rev() {
if map[row][col] > max {
max = map[row][col];
visible[row][col] = true;
if max == 10 {
break;
}
}
}
}
}
fn look_in_right(map: &Map<u8>, visible: &mut Map<bool>, size: usize) {
let mut max;
for row in 0..size {
max = 0_u8;
for col in 0..size {
if map[row][col] > max {
max = map[row][col];
visible[row][col] = true;
if max == 10 {
break;
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
let (map, size) = get_map(data);
let visible = look_in(&map, size);
assert_eq!(21, part_one(&visible));
}
#[test]
fn two() {
let data = include_str!("test.txt");
let (map, size) = get_map(data);
let visible = look_in(&map, size);
assert_eq!(8, part_two(&map, &visible, size, 5));
}
}