/
boneyard7.js
103 lines (93 loc) · 2.9 KB
/
boneyard7.js
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
class File {
constructor([size, name]) {
this.name = name;
this.size = +size;
}
}
class Dir {
constructor(string, parent) {
this.value = string;
this.parent = parent;
this.subDirs = {};
this.files = {};
this.size = 0;
}
ls(array) {
array.forEach((each) => {
if(each[0] === 'dir') {
if (!this.subDirs[each[1]]) {
const newDir = new Dir(each[1], this);
this.subDirs[each[1]] = newDir;
}
} else {
if (!this.files[each[1]]) {
const newFile = new File(each);
this.files[each[1]] = newFile;
this.size = this.size + +each[0];
if (this.value !== '/') {
this.parent.addToParents(+each[0]);
}
}
}
})
}
smallFolders() {
let sumArr = [];
let keys = Object.keys(this.subDirs);
keys.forEach(each => {
const obj = this.subDirs[each];
if (obj.size <= 100000) {
sumArr.push(obj);
};
obj.smallFolders().forEach(eachSub => {
sumArr.push(eachSub);
});
})
return sumArr;
}
addToParents(size) {
this.size = this.size + size;
if (this.parent !== 'root') {
this.parent.addToParents(size);
}
}
}
const root = new Dir('/', 'root')
let pointer = root;
let lsArr = [];
newInput.forEach((each, index, self) => {
if (each[0] === '$') {
if (each[1] === 'cd') {
if (each[2] === '..') {
pointer = pointer.parent
} else if (each[2] === '/') {
pointer = root;
} else if (!pointer.subDirs[each[2]]) {
// if folder doesn't exist, create it
const newDir = new Dir(each[2], pointer)
pointer.subDirs[each[2]] = newDir;
pointer = newDir;
} else {
// folder exists, change pointer
pointer = pointer.subDirs[each[2]]
}
lsArr = [];
} else if (each[1] === 'ls') {
let lsIndex = index + 1;
do {
lsArr.push(self[lsIndex]);
lsIndex++;
} while (self[lsIndex + 1] && self[lsIndex + 1][0] !== '$' && self[lsIndex + 1][0] !== 'cd' && self[lsIndex + 1][0] !== 'ls');
pointer.ls(lsArr);
lsArr = [];
}
}
})
// console.log('root', util.inspect(root, {showHidden: false, depth: null, colors: true}))
console.log('root.smallFolders()', root.smallFolders().map(each => each.value))
let answer = 0;
root.smallFolders().forEach((each) => {
// console.log('each', each)
answer = answer + each.size;
})
console.log('ans', answer)