Skip to content

Commit

Permalink
finish Day 7 pt2
Browse files Browse the repository at this point in the history
  • Loading branch information
meekteek committed Jul 21, 2023
1 parent cd57d52 commit 8700cee
Showing 1 changed file with 38 additions and 12 deletions.
50 changes: 38 additions & 12 deletions src/bin/07.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::{cell::RefCell, collections::HashMap, rc::Rc};

// note: had first created file struct and only during get_total would add to size of struct
// but was extra overhead not necessary for solving problem. Might reintroduce for cleanliness later

const DIRECTORY_CAPACITY: usize = 100000;
const TOTAL_SPACE: usize = 70000000;
const REQUIRED_SPACE: usize = 30000000;

struct BaseDirectory {
root: Rc<Directory>,
Expand All @@ -17,7 +22,7 @@ impl BaseDirectory {
}),
};
}
pub fn parse_part_one(&self, input: &str) {
pub fn parse_input(&self, input: &str) {
let mut current_directory = self.root.clone();
for line in input.lines().skip(1) {
let args = line.split(" ").collect::<Vec<&str>>();
Expand Down Expand Up @@ -61,19 +66,18 @@ struct Directory {
}
impl Directory {
pub fn get_total(&self) -> usize {
let mut total = *self.size.borrow();
total += self
.subdirs
.borrow()
.values()
.fold(0, |acc, subdir| acc + subdir.get_total());
return total;
return *self.size.borrow()
+ self
.subdirs
.borrow()
.values()
.fold(0, |acc, subdir| acc + subdir.get_total());
}
}

pub fn part_one(input: &str) -> Option<u32> {
let base_directory = BaseDirectory::new();
base_directory.parse_part_one(input);
base_directory.parse_input(input);
let mut directories = vec![Rc::clone(&base_directory.root)];
let mut total = 0;
while let Some(dir) = directories.pop() {
Expand All @@ -85,11 +89,31 @@ pub fn part_one(input: &str) -> Option<u32> {
total += size;
}
}
println!("total: {}", total);
return Some(total as u32);
}

pub fn smallest_dir_required(total: usize) -> usize {
let free_space = TOTAL_SPACE - total;
return REQUIRED_SPACE - free_space;
}
pub fn part_two(input: &str) -> Option<u32> {
None
let base_directory = BaseDirectory::new();
base_directory.parse_input(input);
let required_space = smallest_dir_required(base_directory.root.get_total());

let mut directories = vec![Rc::clone(&base_directory.root)];

let mut smallest_required = usize::MAX;
while let Some(dir) = directories.pop() {
for subdir in dir.subdirs.borrow().values() {
directories.push(subdir.clone());
}
let size = dir.get_total();
if size >= required_space {
smallest_required = smallest_required.min(size);
}
}
return Some(smallest_required as u32);
}

fn main() {
Expand All @@ -106,11 +130,13 @@ mod tests {
fn test_part_one() {
let input = advent_of_code::read_file("examples", 7);
println!("{:?}", part_one(&input));
assert_eq!(part_one(&input), Some(95437));
}

#[test]
fn test_part_two() {
let input = advent_of_code::read_file("examples", 7);
assert_eq!(part_two(&input), None);
println!("{:?}", part_two(&input));
assert_eq!(part_two(&input), Some(24933642));
}
}

0 comments on commit 8700cee

Please sign in to comment.