-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7-2.java
205 lines (169 loc) · 5.5 KB
/
day7-2.java
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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Main {
static Directory smallest;
public static void main(String[] aaa) throws FileNotFoundException {
File f = new File("./day7input.txt");
Scanner in = new Scanner(f);
Directory root = new Directory("root", false, 0, null);
Directory current = root;
String s = in.nextLine();
cmd: while (in.hasNextLine()) {
String[] args = s.split(" ");
if (args[0].equals("$")) {
switch (args[1]) {
case "ls":
s = in.nextLine();
continue cmd;
case "cd":
switch (args[2]) {
case "/":
current = root;
case "..":
current = current.backOut();
default:
current = current.getDir(args[2]);
}
s = in.nextLine();
continue cmd;
}
} else {
String s2 = s;
ls: while (in.hasNextLine()) {
args = s2.split(" ");
if (args.length > 2) {
break ls;
}
if (args[0].equals("dir")) {
current.newDir(new Directory(args[1], false, 0, current));
s2 = in.nextLine();
continue ls;
} else {
current.addFile(new Directory(args[1], true, Integer.parseInt(args[0]), current));
s2 = in.nextLine();
continue ls;
}
}
s = s2;
continue cmd;
}
s = in.nextLine();
}
in.close();
int used = calcAll(root);
int free = 70000000 - used;
int required = 30000000 - free;
System.out.println(used + "\n" + free + "\n");
smallest = root;
System.out.println(findSmallestDir(root, required).sizeOfContents);
}
public static Directory findSmallestDir(Directory d, int required) {
for (Directory dir : d.contents) {
if (!dir.isFile) {
int size = dir.calcSizeOfContents();
if (size < smallest.sizeOfContents && size >= required) {
smallest = dir;
} else {
smallest = findSmallestDir(dir, required);
}
} else {
int size = dir.backOut().calcSizeOfContents();
if (size < smallest.sizeOfContents && size >= required) {
smallest = dir.backOut();
}
}
}
return smallest;
}
public static int calcAll(Directory d) {
int sum = 0;
for (Directory dir : d.contents) {
if (!dir.isFile) {
int size = dir.calcSizeOfContents();
// System.out.println(dir.name + " " + size);
sum += size;
System.out.println(dir.name + " " + size + "\n" + sum);
} else {
System.out.println("file " + dir.name + " " + dir.sizeOfFile);
}
}
return sum;
}
}
class Directory {
String name;
Directory parent;
List<Directory> contents = new ArrayList<Directory>();
int sizeOfFile;
int sizeOfContents;
Boolean isFile;
public Directory(String n, Boolean f, int size, Directory parent) {
this.name = n;
this.isFile = f;
this.parent = parent;
if (isFile) {
this.sizeOfFile = size;
}
if (this.name.equals("root")) {
this.sizeOfContents = 46254731;
}
}
public void addFile(Directory d) {
if (!d.isFile) {
System.out.println("not a file.");
} else {
this.contents.add(d);
// calcSizeOfContents();
}
}
public void newDir(Directory d) {
if (d.isFile) {
System.out.println("Not a directory.");
} else {
this.contents.add(d);
// calcSizeOfContents();
}
}
public int calcSizeOfContents() {
int size = 0;
for (Directory dir : this.contents) {
// System.out.println(dir.name);
if (!dir.isFile) {
size += dir.calcSizeOfContents();
} else {
size += dir.sizeOfFile;
// System.out.println("file " + dir.name + " " + dir.sizeOfFile);
}
}
// System.out.println(size);
this.sizeOfContents = size;
return size;
}
public Directory getDir(String name) {
for (Directory dir : this.contents) {
if (dir.name.equals(name) && !dir.isFile) {
return dir;
}
}
return this;
}
public Directory backOut() {
if (this.parent == null)
return this;
return this.parent;
}
public String toString() {
String s = "";
for (Directory dir : this.contents) {
if (!dir.isFile) {
s += " >" + dir + "\n";
} else {
s += dir.name + "\n";
}
}
return s;
}
}