-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay7.java
More file actions
139 lines (125 loc) · 3 KB
/
Day7.java
File metadata and controls
139 lines (125 loc) · 3 KB
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
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
public class Day7 {
// my own readLine to incorporate try catch
public static String myRead(Scanner scanner) {
String line;
try {
line = scanner.nextLine().trim();
} catch (Exception e) {
line = null;
}
return line;
}
public static void main(String[] args) {
File text = new File("/Users/axelteo/Desktop/Day7input.txt");
Scanner scanner;
try {
scanner = new Scanner(text);
} catch (Exception e) {
System.out.println("No scanner");
return;
}
String line = scanner.nextLine().trim();
String[] inputs;
ArrayList<Dir> path = new ArrayList<>();
path.add(new Dir("/"));
while (line != null) {
// possible lines are "$ cd x", "$ cd ..", "$ ls", "1212 abc", and "dir abc"
inputs = line.split(" ", 0);
if (!inputs[0].equals("$")) {
System.out.println("broken");
break;
}
if (inputs[1].equals("cd")) {
if (inputs[2].equals("..")) {
path.remove(path.size() - 1);
} else {
Dir curr = path.get(path.size() - 1);
for (Dir d: curr.getList()) {
if (d.getName().equals(inputs[2])) {
path.add(d);
break;
}
}
}
line = myRead(scanner);
}
if (inputs[1].equals("ls")) {
line = scanner.nextLine().trim();
while (line != null) {
inputs = line.split(" ", 0);
if (inputs[0].equals("$")) {
break;
}
Dir curr = path.get(path.size() - 1);
if (inputs[0].equals("dir")) {
curr.add(new Dir(inputs[1]));
} else {
curr.add(Integer.valueOf(inputs[0]));
}
line = myRead(scanner);
}
}
}
Dir curr = path.get(0);
int currSize = curr.getTotalSize();
int diskSpace = 70000000;
int freeSpace = diskSpace - currSize;
curr.printAllTotalSizesMin(30000000 - freeSpace);
return;
}
}
class Dir {
String name;
int totalSize;
int fileSize;
ArrayList<Dir> arr;
public Dir(String name) {
this.name = name;
this.totalSize = -1;
this.fileSize = 0;
this.arr = new ArrayList<>();
}
public void add(int num) {
this.fileSize += num;
}
public void add(Dir dir) {
this.arr.add(dir);
}
public int getTotalSize() {
if (this.totalSize == -1) {
int tmp = 0;
for (Dir d: arr) {
tmp += d.getTotalSize();
}
tmp += this.fileSize;
this.totalSize = tmp;
}
return this.totalSize;
}
public String getName() {
return this.name;
}
public ArrayList<Dir> getList() {
return this.arr;
}
public void printAllTotalSizesLimit(int n) {
if (this.totalSize < n) {
System.out.println(this.name + ": " + this.totalSize);
}
for(Dir d: this.arr) {
d.printAllTotalSizesLimit(n);
}
}
public void printAllTotalSizesMin(int n) {
if (this.totalSize > n) {
System.out.println(this.name + ": " + this.totalSize);
}
for(Dir d: this.arr) {
d.printAllTotalSizesMin(n);
}
}
}