-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday_07.rs
131 lines (108 loc) Β· 3.07 KB
/
day_07.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use hashbrown::HashSet;
use common::{solution, Answer};
solution!("No Space Left On Device", 7);
fn part_a(input: &str) -> Answer {
process(input)
.get_all_children()
.iter()
.filter(|x| x.is_dir && x.size <= 100000)
.fold(0, |a, x| a + x.size)
.into()
}
fn part_b(input: &str) -> Answer {
let folders = process(input);
let needed_space = 30000000 - (70000000 - folders.size);
let folder_vec = folders.get_all_children();
let mut folder_vec = folder_vec.iter().collect::<Vec<_>>();
folder_vec.sort_by(|a, b| a.size.cmp(&b.size));
folder_vec
.iter()
.find(|x| x.is_dir && x.size > needed_space)
.unwrap()
.size
.into()
}
fn process(raw: &str) -> File {
let mut tree = File::new("root");
let mut path = Vec::new();
for line in raw.lines() {
let parts = line.split_whitespace().collect::<Vec<_>>();
if parts[..2] == ["$", "cd"] {
match parts[2] {
"/" => continue,
".." => {
path.pop().unwrap();
continue;
}
_ => {}
}
let parent = tree.get_path(&path);
path.push(parts[2].to_owned());
if parent.children.iter().any(|x| x.name == parts[2]) {
continue;
}
parent.children.push(File::new(parts[2]));
continue;
}
if parts[0] == "dir" {
let parent = tree.get_path(&path);
if let Some(i) = parent.children.iter_mut().find(|x| x.name == parts[1]) {
i.is_dir = true;
continue;
}
let mut child = File::new(parts[1]);
child.is_dir = true;
parent.children.push(child);
continue;
}
if let Ok(i) = parts[0].parse::<usize>() {
let mut child = File::new(parts[1]);
child.size = i;
tree.get_path(&path).children.push(child);
}
}
tree.propagate_size();
tree
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
struct File {
name: String,
size: usize,
children: Vec<File>,
is_dir: bool,
}
impl File {
fn new(name: &str) -> Self {
Self {
name: name.to_string(),
size: 0,
children: Vec::new(),
is_dir: false,
}
}
fn get_path(&mut self, path: &[String]) -> &mut Self {
let mut current = self;
for part in path {
current = current
.children
.iter_mut()
.find(|f| f.name == *part)
.unwrap();
}
current
}
fn propagate_size(&mut self) -> usize {
for i in &mut self.children {
self.size += i.propagate_size();
}
self.size
}
fn get_all_children(&self) -> HashSet<Self> {
let mut out = HashSet::new();
for i in &self.children {
out.insert(i.clone());
out.extend(i.get_all_children());
}
out
}
}