-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlib.rs
158 lines (137 loc) · 4 KB
/
lib.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
use std::collections::HashSet;
pub fn visible1(input: &str) -> i64 {
let lines: Vec<_> = input.lines().collect();
let trees: Vec<Vec<u32>> = lines
.iter()
.map(|line| line.chars().map(|c| c.to_digit(10).unwrap()).collect())
.collect();
let nb_row = trees.len();
let nb_col = trees.get(0).unwrap().len();
let mut set = HashSet::new();
for col in 0..nb_col {
let mut max = 0;
for row in 0..nb_row {
let v = *trees.get(row).unwrap().get(col).unwrap();
if v > max || row == 0 {
set.insert((row, col));
max = v;
}
}
}
for row in 0..nb_row {
let mut max = 0;
for col in 0..nb_col {
let v = *trees.get(row).unwrap().get(col).unwrap();
if v > max || col == 0 {
set.insert((row, col));
max = v;
}
}
}
for col in 0..nb_col {
let mut max = 0;
for row in (0..nb_row).rev() {
let v = *trees.get(row).unwrap().get(col).unwrap();
if v > max || row == nb_row - 1 {
set.insert((row, col));
max = v;
}
}
}
for row in 0..nb_row {
let mut max = 0;
for col in (0..nb_col).rev() {
let v = *trees.get(row).unwrap().get(col).unwrap();
if v > max || col == nb_col - 1 {
set.insert((row, col));
max = v;
}
}
}
set.len() as i64
}
pub fn visible2(input: &str) -> i64 {
let lines: Vec<_> = input.lines().collect();
let trees: Vec<Vec<u32>> = lines
.iter()
.map(|line| line.chars().map(|c| c.to_digit(10).unwrap()).collect())
.collect();
let nb_row = trees.len();
let nb_col = trees.get(0).unwrap().len();
let mut max_score = 0;
for r in 0..nb_row {
for c in 0..nb_col {
let current_tree = *trees.get(r).unwrap().get(c).unwrap();
let mut score = 1;
// Left
let mut count = 0;
for col in (0..c).rev() {
let n = *trees.get(r).unwrap().get(col).unwrap();
count += 1;
if n >= current_tree {
break;
}
}
score *= count;
// Right
let mut count = 0;
for col in c + 1..nb_col {
let n = *trees.get(r).unwrap().get(col).unwrap();
count += 1;
if n >= current_tree {
break;
}
}
score *= count;
// Top
let mut count = 0;
for row in (0..r).rev() {
let n = *trees.get(row).unwrap().get(c).unwrap();
count += 1;
if n >= current_tree {
break;
}
}
score *= count;
// Bottom
let mut count = 0;
for row in r + 1..nb_row {
let n = *trees.get(row).unwrap().get(c).unwrap();
count += 1;
if n >= current_tree {
break;
}
}
score *= count;
if score > max_score {
max_score = score;
}
}
}
max_score
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
#[test]
fn test_visible1_unit() {
let s = fs::read_to_string("test.txt").unwrap();
assert_eq!(visible1(s.as_str()), 21);
}
#[test]
fn test_visible1_input() {
let s = fs::read_to_string("input.txt").unwrap();
assert_eq!(visible1(s.as_str()), 1679);
}
#[test]
fn test_visible2_unit() {
let s = fs::read_to_string("test.txt").unwrap();
assert_eq!(visible2(s.as_str()), 8);
}
#[test]
fn test_visible2_input() {
let s = fs::read_to_string("input.txt").unwrap();
assert_eq!(visible2(s.as_str()), 536625);
}
}