added completed solution for day 7 part 1 - part 2 still nonfunctional
Changes to be committed: modified: 2022/day07/Day07A.class modified: 2022/day07/Day07A.java modified: 2022/day07/Directory.class modified: 2022/day07/Directory.java
This commit is contained in:
parent
a71c48c5b5
commit
d49d7b5e6e
4 changed files with 32 additions and 73 deletions
Binary file not shown.
|
@ -2,10 +2,12 @@ import java.nio.file.Paths;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
public class Day07A {
|
public class Day07A {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Stack<Directory> breadCrumbs = new Stack<Directory>();
|
Stack<Directory> crumbs = new Stack<Directory>();
|
||||||
try (Scanner inputFile = new Scanner(Paths.get("input.txt"))) {
|
try (Scanner inputFile = new Scanner(Paths.get("input.txt"))) {
|
||||||
boolean passingBack = false;
|
boolean passingBack = false;
|
||||||
String[] command = new String[3];
|
String[] command = new String[3];
|
||||||
|
@ -17,11 +19,11 @@ public class Day07A {
|
||||||
switch (command[1]) {
|
switch (command[1]) {
|
||||||
case "cd": {
|
case "cd": {
|
||||||
if (command[2].compareTo("..") == 0) {
|
if (command[2].compareTo("..") == 0) {
|
||||||
breadCrumbs.pop();
|
crumbs.pop();
|
||||||
} else if (breadCrumbs.empty()) {
|
} else if (crumbs.empty()) {
|
||||||
breadCrumbs.push(new Directory(command[2], command[2]));
|
crumbs.push(new Directory(command[2], command[2]));
|
||||||
} else {
|
} else {
|
||||||
breadCrumbs.push(breadCrumbs.peek().moveToSubDir(command[2]));
|
crumbs.push(crumbs.peek().moveToSubDir(command[2]));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -29,11 +31,11 @@ public class Day07A {
|
||||||
command = inputFile.nextLine().split(" ");
|
command = inputFile.nextLine().split(" ");
|
||||||
while (!inputFile.hasNext("$") && inputFile.hasNext()) {
|
while (!inputFile.hasNext("$") && inputFile.hasNext()) {
|
||||||
if (command[0].compareTo("dir") == 0) {
|
if (command[0].compareTo("dir") == 0) {
|
||||||
breadCrumbs.peek().addDir(command[1]);
|
crumbs.peek().addDir(command[1]);
|
||||||
} else if (command[0].compareTo("$") == 0) {
|
} else if (command[0].compareTo("$") == 0) {
|
||||||
break;
|
break;
|
||||||
} else{
|
} else{
|
||||||
breadCrumbs.peek().addFile(Integer.parseInt(command[0]));
|
crumbs.peek().addFile(Integer.parseInt(command[0]));
|
||||||
}
|
}
|
||||||
command = inputFile.nextLine().split(" ");
|
command = inputFile.nextLine().split(" ");
|
||||||
}
|
}
|
||||||
|
@ -42,9 +44,24 @@ public class Day07A {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {e.printStackTrace();}
|
} catch (IOException e) {e.printStackTrace();}
|
||||||
while (breadCrumbs.size() > 1) {
|
while (crumbs.size() > 1) {
|
||||||
breadCrumbs.pop();
|
crumbs.pop();
|
||||||
}
|
}
|
||||||
|
ArrayList<Integer> sizes = crumbs.peek().sizeHierarchy();
|
||||||
|
int sum = 0;
|
||||||
|
for (Integer i : sizes) {
|
||||||
|
if (i <= 100000) {
|
||||||
|
sum += i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println(sum);
|
||||||
|
|
||||||
|
Stack<Integer> sortList = new Stack<Integer>();
|
||||||
|
for (Integer i : sizes) {
|
||||||
|
if (i >= 30000000 - (70000000 - Collections.max(sizes))) {
|
||||||
|
sortList.push(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println(Collections.min(sortList));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Binary file not shown.
|
@ -16,29 +16,6 @@ public class Directory {
|
||||||
this.parentDirName = parent;
|
this.parentDirName = parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Directory (String name, String parent, ArrayList<Integer> filesList){
|
|
||||||
this.dirName = name;
|
|
||||||
this.parentDirName = parent;
|
|
||||||
this.files.addAll(filesList);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Directory (String name, String parent, String[] dirsList){
|
|
||||||
this.dirName = name;
|
|
||||||
this.parentDirName = parent;
|
|
||||||
for (String s : dirsList) {
|
|
||||||
this.addDir(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Directory (String name, String parent, ArrayList<Integer> filesList, String[] dirsList){
|
|
||||||
this.dirName = name;
|
|
||||||
this.parentDirName = parent;
|
|
||||||
this.files.addAll(filesList);
|
|
||||||
for (String s : dirsList) {
|
|
||||||
this.addDir(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getDirSize() {
|
public int getDirSize() {
|
||||||
this.dirSize = this.calcDirSize();
|
this.dirSize = this.calcDirSize();
|
||||||
return this.dirSize;
|
return this.dirSize;
|
||||||
|
@ -59,16 +36,6 @@ public class Directory {
|
||||||
this.subDirs.add(new Directory(s, this.dirName));
|
this.subDirs.add(new Directory(s, this.dirName));
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Directory> getSubsOfCurrentDir() {
|
|
||||||
this.dirSize = this.calcDirSize();
|
|
||||||
return this.subDirs;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addDir(String s, ArrayList<Integer> i) {
|
|
||||||
this.subDirs.add(new Directory(s, this.dirName));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addFile(int i) {
|
public void addFile(int i) {
|
||||||
this.files.add(i);
|
this.files.add(i);
|
||||||
}
|
}
|
||||||
|
@ -77,15 +44,6 @@ public class Directory {
|
||||||
return this.dirName;
|
return this.dirName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Directory> getRecursiveSubDirs() {
|
|
||||||
ArrayList<Directory> arr = new ArrayList<Directory>();
|
|
||||||
this.dirSize = this.calcDirSize();
|
|
||||||
for (Directory dir : subDirs) {
|
|
||||||
arr.addAll(dir.getRecursiveSubDirs());
|
|
||||||
}
|
|
||||||
return arr;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Directory moveToSubDir(String s) {
|
public Directory moveToSubDir(String s) {
|
||||||
Directory retDir = new Directory();
|
Directory retDir = new Directory();
|
||||||
Boolean foundFlag = false;
|
Boolean foundFlag = false;
|
||||||
|
@ -102,28 +60,12 @@ public class Directory {
|
||||||
return retDir;
|
return retDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public ArrayList<Integer> sizeHierarchy() {
|
||||||
StringBuilder s = new StringBuilder();
|
ArrayList<Integer> s = new ArrayList<Integer>();
|
||||||
for (Integer i : files) {
|
s.add(this.getDirSize());
|
||||||
s.append(i.toString()+"\n");
|
|
||||||
}
|
|
||||||
for (Directory d : subDirs) {
|
for (Directory d : subDirs) {
|
||||||
s.append(d.getDirName()+"\n");
|
s.addAll(d.sizeHierarchy());
|
||||||
}
|
}
|
||||||
return s.toString();
|
return s;
|
||||||
}
|
|
||||||
|
|
||||||
public String fullHierarchyString() {
|
|
||||||
StringBuilder s = new StringBuilder();
|
|
||||||
for (Integer i : files) {
|
|
||||||
s.append(i.toString()+"\n");
|
|
||||||
}
|
|
||||||
for (Directory d : subDirs) {
|
|
||||||
s.append(d.getDirName()+"\n");
|
|
||||||
}
|
|
||||||
for (Directory d : subDirs) {
|
|
||||||
s.append(d.fullHierarchyString());
|
|
||||||
}
|
|
||||||
return s.toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue