-
Notifications
You must be signed in to change notification settings - Fork 0
/
day07_no_space_left_on_device.rs
231 lines (221 loc) · 6.54 KB
/
day07_no_space_left_on_device.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use self::Node::*;
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::fs::read_to_string;
use std::rc::{Rc, Weak};
fn part_1(input: &str) -> u64 {
let root = build_fs(input);
let mut nodes = vec![root];
let mut sum = 0;
while !nodes.is_empty() {
nodes = nodes
.iter()
.filter(|n| matches!(***n, Dir { .. }))
.inspect(|n| {
let size = n.dir_info().unwrap().0.get().unwrap();
if size < 100000 {
sum += size;
}
})
.map(|n| {
let (.., entrys) = n.dir_info().unwrap();
entrys
.borrow()
.values()
.map(|rc| rc.clone())
.collect::<Vec<_>>()
})
.flatten()
.collect::<Vec<_>>();
}
sum
}
fn part_2(input: &str) -> u64 {
let root = build_fs(input);
let used = root.dir_info().unwrap().0.get().unwrap();
let need = 30000000 - (70000000 - used);
let mut nodes = vec![root];
let mut min_delete = used;
while !nodes.is_empty() {
nodes = nodes
.iter()
.filter(|n| matches!(***n, Dir { .. }))
.inspect(|n| {
let size = n.dir_info().unwrap().0.get().unwrap();
if size > need && size < min_delete {
min_delete = size;
}
})
.map(|n| {
let (.., entrys) = n.dir_info().unwrap();
entrys
.borrow()
.values()
.map(|rc| rc.clone())
.collect::<Vec<_>>()
})
.flatten()
.collect::<Vec<_>>();
}
min_delete
}
enum Node {
Dir {
size: Cell<Option<u64>>,
parent: Option<Weak<Node>>,
entrys: RefCell<HashMap<String, Rc<Node>>>,
},
File(u64),
}
impl Node {
fn dir_info<'a>(
self: &'a Rc<Self>,
) -> Option<(
&'a Cell<Option<u64>>,
&'a Option<Weak<Node>>,
&'a RefCell<HashMap<String, Rc<Node>>>,
)> {
if let Dir {
size,
parent,
entrys,
} = &**self
{
return Some((size, parent, entrys));
}
None
}
fn cal_dir_size(self: &Rc<Self>) {
let (size, _, entrys) = self.dir_info().unwrap();
if size.get().is_some() {
return;
}
let mut sum = 0;
for node in entrys.borrow().values() {
match &**node {
File(n) => sum += *n,
Dir { size: s, .. } if s.get().is_some() => sum += s.get().unwrap(),
_ => return,
}
}
size.set(Some(sum));
}
}
fn build_fs(input: &str) -> Rc<Node> {
let root = Rc::new(Dir {
parent: None,
entrys: RefCell::new(HashMap::new()),
size: Cell::new(None),
});
let mut wd = root.clone();
for line in input.trim().lines().skip(1) {
match line {
"$ ls" => continue,
"$ cd /" => {
wd.cal_dir_size();
wd = root.clone();
continue;
}
"$ cd .." => {
wd.cal_dir_size();
let parent = wd.dir_info().unwrap().1.as_ref();
wd = parent.map(|p| p.upgrade().unwrap()).unwrap_or(root.clone());
continue;
}
_ => (),
}
let (left, right) = line.rsplit_once(' ').unwrap();
match left {
"$ cd" => {
let (.., entrys) = wd.dir_info().unwrap();
let n = entrys.borrow().get(right).unwrap().clone();
wd = n;
}
"dir" => {
let (.., entrys) = wd.dir_info().unwrap();
entrys.borrow_mut().entry(right.into()).or_insert_with(|| {
Rc::new(Dir {
parent: Some(Rc::downgrade(&wd)),
entrys: RefCell::new(HashMap::new()),
size: Cell::new(None),
})
});
}
_ => {
let mut entrys = wd.dir_info().unwrap().2.borrow_mut();
entrys
.entry(right.into())
.or_insert_with(|| Rc::new(File(left.parse().unwrap())));
}
}
}
while !Rc::ptr_eq(&wd, &root) {
wd.cal_dir_size();
let parent = wd.dir_info().unwrap().1.as_ref();
wd = parent.map(|p| p.upgrade().unwrap()).unwrap_or(root.clone())
}
root.cal_dir_size();
root
}
pub fn print_answer() {
let input = read_to_string("./src/aoc_2022/data/input_2022_07").unwrap();
println!(
"Part 1: {}\nPart 2: {}",
part_1(&input.trim()),
part_2(&input.trim())
)
}
#[test]
fn part_1_test() {
let input: &str = "$ cd /\n\
$ ls\n\
dir a\n\
14848514 b.txt\n\
8504156 c.dat\n\
dir d\n\
$ cd a\n\
$ ls\n\
dir e\n\
29116 f\n\
2557 g\n\
62596 h.lst\n\
$ cd e\n\
$ ls\n\
584 i\n\
$ cd ..\n\
$ cd ..\n\
$ cd d\n\
$ ls\n\
4060174 j\n\
8033020 d.log\n\
5626152 d.ext\n\
7214296 k";
assert_eq!(part_1(input), 95437);
}
#[test]
fn part_2_test() {
let input: &str = "$ cd /\n\
$ ls\n\
dir a\n\
14848514 b.txt\n\
8504156 c.dat\n\
dir d\n\
$ cd a\n\
$ ls\n\
dir e\n\
29116 f\n\
2557 g\n\
62596 h.lst\n\
$ cd e\n\
$ ls\n\
584 i\n\
$ cd ..\n\
$ cd ..\n\
$ cd d\n\
$ ls\n\
4060174 j\n\
8033020 d.log\n\
5626152 d.ext\n\
7214296 k";
assert_eq!(part_2(input), 24933642);
}