Skip to content

Commit 97b1161

Browse files
committed
Part 2 solved
1 parent a94feec commit 97b1161

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

src/day_07.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,18 @@ use crate::input_reader;
22
use std::{collections::HashMap};
33

44
struct File {
5-
name: String,
65
size: usize
76
}
87

9-
fn finished_mapping(
10-
dirs: &Vec<Vec<&str>>,
11-
dir_size: &HashMap<Vec<&str>, usize>
12-
) -> bool {
13-
for dir in dirs {
14-
if !dir_size.contains_key(dir) {
15-
return false;
16-
}
17-
}
18-
return true;
19-
}
20-
21-
fn part1(
8+
fn part1and2(
229
sub_dirs: HashMap<Vec<&str>, Vec<Vec<&str>>>,
2310
files: HashMap<Vec<&str>, Vec<File>>,
2411
root: Vec<&str>
2512
) {
2613
let mut dir_size: HashMap<Vec<&str>, usize> = HashMap::new();
2714
let cursor: &mut Vec<&str> = &mut root.clone();
2815

29-
'map_sub_dir: while !finished_mapping(sub_dirs.get(&root.clone()).unwrap(), &dir_size) {
16+
'map_sub_dir: while dir_size.get(&root).is_none() {
3017

3118
let mut sum: usize = 0;
3219

@@ -54,13 +41,24 @@ fn part1(
5441
}
5542

5643
let mut sum: usize = 0;
57-
for d in dir_size {
58-
if d.1 <= 100000 {
59-
sum += d.1;
44+
for d in &dir_size {
45+
if *d.1 <= 100000 {
46+
sum += *d.1;
6047
}
6148
}
6249

6350
println!("Part 1: {}", sum);
51+
52+
let unused_space = 70000000 - *dir_size.get(&root).unwrap();
53+
let to_free = 30000000 - unused_space;
54+
let mut min: usize = std::usize::MAX;
55+
for d in &dir_size {
56+
if *d.1 < min && *d.1 >= to_free {
57+
min = *d.1;
58+
}
59+
}
60+
61+
println!("Part 2: {}", min);
6462
}
6563

6664
pub fn run() {
@@ -113,8 +111,7 @@ pub fn run() {
113111
path_clone.push(sub_dir_name);
114112
dir_dirs.push(path_clone);
115113
} else if let Some(file_size) = item_info[0].parse::<usize>().ok() {
116-
let file_name = item_info[1];
117-
let file = File { size: file_size, name: file_name.to_string() };
114+
let file = File { size: file_size };
118115
dir_files.push(file);
119116
}
120117
}
@@ -125,5 +122,5 @@ pub fn run() {
125122
_ => { /* Do nothing if command is not recognized */ }
126123
};
127124
}
128-
part1(dirs, files, vec!["/"]);
125+
part1and2(dirs, files, vec!["/"]);
129126
}

0 commit comments

Comments
 (0)