diff options
Diffstat (limited to '2021/day9/src/main.rs')
-rw-r--r-- | 2021/day9/src/main.rs | 49 |
1 files changed, 18 insertions, 31 deletions
diff --git a/2021/day9/src/main.rs b/2021/day9/src/main.rs index 3c8d4c6..5d84be9 100644 --- a/2021/day9/src/main.rs +++ b/2021/day9/src/main.rs | |||
@@ -39,25 +39,19 @@ fn main() { | |||
39 | for (ndx, num) in height.iter().enumerate() { | 39 | for (ndx, num) in height.iter().enumerate() { |
40 | let up = points | 40 | let up = points |
41 | .get(((idx as isize) - 1) as usize) | 41 | .get(((idx as isize) - 1) as usize) |
42 | .unwrap_or_else(|| &nine) | 42 | .unwrap_or(&nine) |
43 | .get(ndx) | 43 | .get(ndx) |
44 | .unwrap_or_else(|| &9); | 44 | .unwrap_or(&9); |
45 | 45 | ||
46 | let down = match points.get(idx + 1) { | 46 | let down = match points.get(idx + 1) { |
47 | Some(line) => match line.get(ndx) { | 47 | Some(line) => line.get(ndx).unwrap_or(&9), |
48 | Some(number) => number, | ||
49 | None => &9, | ||
50 | }, | ||
51 | None => &9, | ||
52 | }; | ||
53 | let left = match height.get((ndx as isize - 1) as usize) { | ||
54 | Some(number) => number, | ||
55 | None => &9, | ||
56 | }; | ||
57 | let right = match height.get(ndx + 1) { | ||
58 | Some(number) => number, | ||
59 | None => &9, | 48 | None => &9, |
60 | }; | 49 | }; |
50 | |||
51 | let left = height.get((ndx as isize - 1) as usize).unwrap_or(&9); | ||
52 | |||
53 | let right = height.get(ndx + 1).unwrap_or(&9); | ||
54 | |||
61 | if num < up && num < down && num < right && num < left { | 55 | if num < up && num < down && num < right && num < left { |
62 | risk_level += 1 + *num; | 56 | risk_level += 1 + *num; |
63 | basins.push(Basin { | 57 | basins.push(Basin { |
@@ -81,29 +75,22 @@ fn main() { | |||
81 | 75 | ||
82 | let up = points | 76 | let up = points |
83 | .get(((minibasin.x as isize) - 1) as usize) | 77 | .get(((minibasin.x as isize) - 1) as usize) |
84 | .unwrap_or_else(|| &nine) | 78 | .unwrap_or(&nine) |
85 | .get(minibasin.y) | 79 | .get(minibasin.y) |
86 | .unwrap_or_else(|| &9); | 80 | .unwrap_or(&9); |
87 | 81 | ||
88 | let down = match points.get(minibasin.x + 1) { | 82 | let down = match points.get(minibasin.x + 1) { |
89 | Some(line) => match line.get(minibasin.y) { | 83 | Some(line) => line.get(minibasin.y).unwrap_or(&9), |
90 | Some(number) => number, | ||
91 | None => &9, | ||
92 | }, | ||
93 | None => &9, | 84 | None => &9, |
94 | }; | 85 | }; |
95 | let left = match points.get(minibasin.x) { | 86 | let left = match points.get(minibasin.x) { |
96 | Some(line) => match line.get(((minibasin.y as isize) - 1) as usize) { | 87 | Some(line) => line |
97 | Some(number) => number, | 88 | .get(((minibasin.y as isize) - 1) as usize) |
98 | None => &9, | 89 | .unwrap_or(&9), |
99 | }, | ||
100 | None => &9, | 90 | None => &9, |
101 | }; | 91 | }; |
102 | let right = match points.get(minibasin.x) { | 92 | let right = match points.get(minibasin.x) { |
103 | Some(line) => match line.get(minibasin.y + 1) { | 93 | Some(line) => line.get(minibasin.y + 1).unwrap_or(&9), |
104 | Some(number) => number, | ||
105 | None => &9, | ||
106 | }, | ||
107 | None => &9, | 94 | None => &9, |
108 | }; | 95 | }; |
109 | 96 | ||
@@ -163,12 +150,12 @@ fn main() { | |||
163 | 150 | ||
164 | let mut lens: Vec<usize> = basins.iter().map(|x| x.points.len()).collect(); | 151 | let mut lens: Vec<usize> = basins.iter().map(|x| x.points.len()).collect(); |
165 | 152 | ||
166 | lens.sort(); | 153 | lens.sort_unstable(); |
167 | lens.reverse(); | 154 | lens.reverse(); |
168 | 155 | ||
169 | let foo: usize = lens.iter().take(3).product(); | 156 | let biggest_basins: usize = lens.iter().take(3).product(); |
170 | 157 | ||
171 | println!("{}", foo); | 158 | println!("{}", biggest_basins); |
172 | println!("{}", risk_level); | 159 | println!("{}", risk_level); |
173 | } | 160 | } |
174 | 161 | ||