summaryrefslogtreecommitdiffstats
path: root/2021/day9/src
diff options
context:
space:
mode:
Diffstat (limited to '2021/day9/src')
-rw-r--r--2021/day9/src/main.rs120
1 files changed, 120 insertions, 0 deletions
diff --git a/2021/day9/src/main.rs b/2021/day9/src/main.rs
index 75bf1e9..3c8d4c6 100644
--- a/2021/day9/src/main.rs
+++ b/2021/day9/src/main.rs
@@ -1,11 +1,25 @@
1use std::collections::HashSet;
1use std::env; 2use std::env;
2use std::fs::File; 3use std::fs::File;
3use std::io::{BufRead, BufReader}; 4use std::io::{BufRead, BufReader};
4 5
6#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
7struct Point {
8 x: usize,
9 y: usize,
10 height: u32,
11}
12
13#[derive(Debug, Clone, PartialEq, Eq)]
14struct Basin {
15 points: HashSet<Point>,
16}
17
5fn main() { 18fn main() {
6 let reader = open_file(); 19 let reader = open_file();
7 20
8 let mut points: Vec<Vec<u32>> = Vec::new(); 21 let mut points: Vec<Vec<u32>> = Vec::new();
22 let mut basins: Vec<Basin> = Vec::new();
9 23
10 for line in reader.lines() { 24 for line in reader.lines() {
11 points.push( 25 points.push(
@@ -46,9 +60,115 @@ fn main() {
46 }; 60 };
47 if num < up && num < down && num < right && num < left { 61 if num < up && num < down && num < right && num < left {
48 risk_level += 1 + *num; 62 risk_level += 1 + *num;
63 basins.push(Basin {
64 points: HashSet::from_iter(vec![Point {
65 x: idx,
66 y: ndx,
67 height: *num,
68 }]),
69 });
70 }
71 }
72 }
73
74 // TODO: fix this maybe because i hate it <20-12-21, yigit> //
75 for basin in basins.iter_mut() {
76 let mut stack: Vec<Point> = Vec::new();
77 stack.extend(basin.points.clone().into_iter());
78
79 while !stack.is_empty() {
80 let minibasin = stack.pop().unwrap();
81
82 let up = points
83 .get(((minibasin.x as isize) - 1) as usize)
84 .unwrap_or_else(|| &nine)
85 .get(minibasin.y)
86 .unwrap_or_else(|| &9);
87
88 let down = match points.get(minibasin.x + 1) {
89 Some(line) => match line.get(minibasin.y) {
90 Some(number) => number,
91 None => &9,
92 },
93 None => &9,
94 };
95 let left = match points.get(minibasin.x) {
96 Some(line) => match line.get(((minibasin.y as isize) - 1) as usize) {
97 Some(number) => number,
98 None => &9,
99 },
100 None => &9,
101 };
102 let right = match points.get(minibasin.x) {
103 Some(line) => match line.get(minibasin.y + 1) {
104 Some(number) => number,
105 None => &9,
106 },
107 None => &9,
108 };
109
110 if up != &9 {
111 let p = Point {
112 x: minibasin.x - 1,
113 y: minibasin.y,
114 height: *up,
115 };
116
117 if !basin.points.contains(&p) {
118 stack.push(p);
119 basin.points.insert(p);
120 }
121 }
122
123 if down != &9 {
124 let p = Point {
125 x: minibasin.x + 1,
126 y: minibasin.y,
127 height: *down,
128 };
129
130 if !basin.points.contains(&p) {
131 stack.push(p);
132 basin.points.insert(p);
133 }
134 }
135
136 if left != &9 {
137 let p = Point {
138 x: minibasin.x,
139 y: minibasin.y - 1,
140 height: *left,
141 };
142
143 if !basin.points.contains(&p) {
144 stack.push(p);
145 basin.points.insert(p);
146 }
147 }
148
149 if right != &9 {
150 let p = Point {
151 x: minibasin.x,
152 y: minibasin.y + 1,
153 height: *right,
154 };
155
156 if !basin.points.contains(&p) {
157 stack.push(p);
158 basin.points.insert(p);
159 }
49 } 160 }
50 } 161 }
51 } 162 }
163
164 let mut lens: Vec<usize> = basins.iter().map(|x| x.points.len()).collect();
165
166 lens.sort();
167 lens.reverse();
168
169 let foo: usize = lens.iter().take(3).product();
170
171 println!("{}", foo);
52 println!("{}", risk_level); 172 println!("{}", risk_level);
53} 173}
54 174