-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.rs
193 lines (169 loc) · 4.66 KB
/
day08.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
use std::collections::HashSet;
fn visible_from_outside(grid: &Vec<Vec<usize>>) -> HashSet<(usize, usize)> {
let mut visible = HashSet::new();
// Any size map of (height, width)
let max = (grid[0].len(), grid.len());
// Scan horizontal
for e in 1..max.0 - 1 {
visible.extend(peek_south(&grid, (0, e), max.1));
visible.extend(peek_north(&grid, (max.1 - 1, e)));
}
// Scan vertical
for s in 1..max.1 - 1 {
visible.extend(peek_east(&grid, (s, 0), max.0));
visible.extend(peek_west(&grid, (s, max.0 - 1)));
}
// Include perimeter
for i in 0..max.0 {
visible.insert((i, 0));
visible.insert((i, max.1 - 1));
}
for i in 0..max.1 {
visible.insert((0, i));
visible.insert((max.0 - 1, i));
}
visible
}
fn peek_north(grid: &Vec<Vec<usize>>, loc: (usize, usize)) -> HashSet<(usize, usize)> {
let mut height = grid[loc.0][loc.1];
let mut visible = HashSet::new();
for row in (1..loc.0-1).rev() {
if height < grid[row][loc.1] {
height = grid[row][loc.1];
visible.insert((row, loc.1));
}
}
visible
}
fn peek_south(grid: &Vec<Vec<usize>>, loc: (usize, usize), map_h: usize) -> HashSet<(usize, usize)> {
let mut height = grid[loc.0][loc.1];
let mut visible = HashSet::new();
for row in loc.0 + 1..map_h-1 {
if height < grid[row][loc.1] {
height = grid[row][loc.1];
visible.insert((row, loc.1));
}
}
visible
}
fn peek_east(grid: &Vec<Vec<usize>>, loc: (usize, usize), map_w: usize) -> HashSet<(usize, usize)> {
let mut height = grid[loc.0][loc.1];
let mut visible = HashSet::new();
for col in loc.1 + 1..map_w-1 {
if height < grid[loc.0][col] {
height = grid[loc.0][col];
visible.insert((loc.0, col));
}
}
visible
}
fn peek_west(grid: &Vec<Vec<usize>>, loc: (usize, usize)) -> HashSet<(usize, usize)> {
let mut height = grid[loc.0][loc.1];
let mut visible = HashSet::new();
for col in (1..loc.1-1).rev() {
if height < grid[loc.0][col] {
height = grid[loc.0][col];
visible.insert((loc.0, col));
}
}
visible
}
fn best_scene(grid: &Vec<Vec<usize>>) -> usize {
let mut best_score = 0;
for row in 0..grid.len() {
for col in 0..grid[1].len() {
let rating = get_rating(grid, row, col);
if rating > best_score {
best_score = rating;
}
}
}
best_score
}
fn get_rating(grid: &Vec<Vec<usize>>, row: usize, col: usize) -> usize {
let dirs = [see_down, see_up, see_right, see_left];
dirs.iter().map(|f| f(grid, row, col)).product()
}
fn see_up(grid: &Vec<Vec<usize>>, row: usize, col: usize) -> usize {
let mut score = 0;
let val = grid[row][col];
for i in (0..row).rev() {
score += 1;
if val <= grid[i][col] {
break;
}
}
score
}
fn see_down(grid: &Vec<Vec<usize>>, row: usize, col: usize) -> usize {
let map_height = grid.len();
let mut score = 0;
let val = grid[row][col];
for i in row + 1..map_height {
score += 1;
if val <= grid[i][col] {
break;
}
}
score
}
fn see_right(grid: &Vec<Vec<usize>>, row: usize, col: usize) -> usize {
let map_width = grid[0].len();
let mut score = 0;
let val = grid[row][col];
for j in col + 1..map_width {
score += 1;
if val <= grid[row][j] {
break;
}
}
score
}
fn see_left(grid: &Vec<Vec<usize>>, row: usize, col: usize) -> usize {
let mut score = 0;
let val = grid[row][col];
for j in (0..col).rev() {
score += 1;
if val <= grid[row][j] {
break;
}
}
score
}
#[aoc_generator(day8)]
pub fn input_generator(input: &str) -> Vec<Vec<usize>> {
input
.lines()
.map(|line| {
line.trim()
.chars()
.map(|c| c.to_digit(10).unwrap() as usize)
.collect()
})
.collect()
}
#[aoc(day8, part1)]
pub fn solve_part1(input: &Vec<Vec<usize>>) -> usize {
visible_from_outside(input).len()
}
#[aoc(day8, part2)]
pub fn solve_part2(input: &Vec<Vec<usize>>) -> usize {
best_scene(input)
}
#[cfg(test)]
mod tests {
use super::*;
const TEST: &str = "30373
25512
65332
33549
35390";
#[test]
fn part1() {
assert_eq!(solve_part1(&input_generator(TEST)), 21);
}
#[test]
fn part2() {
assert_eq!(solve_part2(&input_generator(TEST)), 8);
}
}