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
|
use std::collections::HashSet;
use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader};
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
struct Point {
x: usize,
y: usize,
height: u32,
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct Basin {
points: HashSet<Point>,
}
fn main() {
let reader = open_file();
let mut points: Vec<Vec<u32>> = Vec::new();
let mut basins: Vec<Basin> = Vec::new();
for line in reader.lines() {
points.push(
line.unwrap()
.trim()
.chars()
.map(|x| x.to_digit(10).unwrap())
.collect(),
);
}
let nine = vec![9];
let mut risk_level: u32 = 0;
for (idx, height) in points.iter().enumerate() {
for (ndx, num) in height.iter().enumerate() {
let up = points
.get(((idx as isize) - 1) as usize)
.unwrap_or(&nine)
.get(ndx)
.unwrap_or(&9);
let down = points
.get(idx + 1)
.unwrap_or_else(|| &nine)
.get(ndx)
.unwrap_or_else(|| &9);
let left = height
.get((ndx as isize - 1) as usize)
.unwrap_or_else(|| &9);
let right = height
.get((ndx as isize + 1) as usize)
.unwrap_or_else(|| &9);
if num < up && num < down && num < right && num < left {
risk_level += 1 + *num;
basins.push(Basin {
points: HashSet::from_iter(vec![Point {
x: idx,
y: ndx,
height: *num,
}]),
});
}
}
}
// TODO: fix this maybe because i hate it <20-12-21, yigit> //
for basin in basins.iter_mut() {
let mut stack: Vec<Point> = Vec::new();
stack.extend(basin.points.clone().into_iter());
while !stack.is_empty() {
let minibasin = stack.pop().unwrap();
let up = points
.get(((minibasin.x as isize) - 1) as usize)
.unwrap_or(&nine)
.get(minibasin.y)
.unwrap_or(&9);
let down = match points.get(minibasin.x + 1) {
Some(line) => line.get(minibasin.y).unwrap_or(&9),
None => &9,
};
let left = match points.get(minibasin.x) {
Some(line) => line
.get(((minibasin.y as isize) - 1) as usize)
.unwrap_or(&9),
None => &9,
};
let right = match points.get(minibasin.x) {
Some(line) => line.get(minibasin.y + 1).unwrap_or(&9),
None => &9,
};
if up != &9 {
let p = Point {
x: minibasin.x - 1,
y: minibasin.y,
height: *up,
};
if !basin.points.contains(&p) {
stack.push(p);
basin.points.insert(p);
}
}
if down != &9 {
let p = Point {
x: minibasin.x + 1,
y: minibasin.y,
height: *down,
};
if !basin.points.contains(&p) {
stack.push(p);
basin.points.insert(p);
}
}
if left != &9 {
let p = Point {
x: minibasin.x,
y: minibasin.y - 1,
height: *left,
};
if !basin.points.contains(&p) {
stack.push(p);
basin.points.insert(p);
}
}
if right != &9 {
let p = Point {
x: minibasin.x,
y: minibasin.y + 1,
height: *right,
};
if !basin.points.contains(&p) {
stack.push(p);
basin.points.insert(p);
}
}
}
}
let mut lens: Vec<usize> = basins.iter().map(|x| x.points.len()).collect();
lens.sort_unstable();
lens.reverse();
let biggest_basins: usize = lens.iter().take(3).product();
println!("{}", biggest_basins);
println!("{}", risk_level);
}
fn open_file() -> BufReader<File> {
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
eprintln!("Usage: {} <filename>", args[0]);
std::process::exit(1);
}
let filename = &args[1];
let file = File::open(filename).unwrap_or_else(|_| panic!("No such file: {}", filename));
BufReader::new(file)
}
|