From 3621b8231a603854e6c088df2879bae02e62681d Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Mon, 20 Dec 2021 15:56:27 +0300 Subject: 2021, day9: part2 --- 2021/day9/src/main.rs | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) (limited to '2021') 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 @@ +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, +} + fn main() { let reader = open_file(); let mut points: Vec> = Vec::new(); + let mut basins: Vec = Vec::new(); for line in reader.lines() { points.push( @@ -46,9 +60,115 @@ fn main() { }; 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 = 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_else(|| &nine) + .get(minibasin.y) + .unwrap_or_else(|| &9); + + let down = match points.get(minibasin.x + 1) { + Some(line) => match line.get(minibasin.y) { + Some(number) => number, + None => &9, + }, + None => &9, + }; + let left = match points.get(minibasin.x) { + Some(line) => match line.get(((minibasin.y as isize) - 1) as usize) { + Some(number) => number, + None => &9, + }, + None => &9, + }; + let right = match points.get(minibasin.x) { + Some(line) => match line.get(minibasin.y + 1) { + Some(number) => number, + None => &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 = basins.iter().map(|x| x.points.len()).collect(); + + lens.sort(); + lens.reverse(); + + let foo: usize = lens.iter().take(3).product(); + + println!("{}", foo); println!("{}", risk_level); } -- cgit v1.2.3-70-g09d2