day07 part 1

This commit is contained in:
2022-12-07 12:02:44 +01:00
parent a4079ac7dc
commit 57150f3fde
8 changed files with 1389 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
package com.felixalb.app;
import java.io.*;
import java.util.*;
import java.nio.file.*;
public class App {
private static String[] readLines(String filename) {
try {
return Files.readAllLines(Paths.get(filename)).toArray(new String[0]);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Usage: java App <filename>");
return;
}
String filename = args[0];
String[] lines = readLines(filename);
System.out.println("Lines: " + lines.length);
FileSystemReader fileSystemReader = new FileSystemReader(lines);
// Part 1
FileNode p1_root = fileSystemReader.getRoot();
Integer directorySizeSum = 0;
ArrayList<Integer> p1_sizes = p1_root.getDirectorySizesFlat(new ArrayList<Integer>());
// System.out.println("Part 1: " + p1_sizes);
for (Integer size : p1_sizes) {
if (size <= 100000) {
directorySizeSum += size;
}
}
p1_root.printNode("");
System.out.println("Part 1: " + directorySizeSum);
}
}

View File

@@ -0,0 +1,70 @@
package com.felixalb.app;
import java.util.ArrayList;
class FileNode {
private String name;
private Integer size;
private Boolean isDirectory;
private ArrayList<FileNode> children = new ArrayList<FileNode>();
public FileNode(String name, Boolean isDirectory) {
this.name = name;
this.children = new ArrayList<FileNode>();
this.isDirectory = isDirectory;
}
public Integer getSize() {
if (this.isDirectory) {
Integer size = 0;
for (FileNode child : this.children) {
size += child.getSize();
}
return size;
} else {
return this.size;
}
}
public void setSize(Integer size) {
if (this.isDirectory) {
throw new RuntimeException("Cannot set size of directory");
}
this.size = size;
}
public String getName() {
return name;
}
public ArrayList<FileNode> getChildren() {
if (!this.isDirectory) {
throw new RuntimeException("Cannot get children of file");
}
return this.children;
}
public void appendChild(FileNode child) {
this.children.add(child);
}
public void printNode(String prefix) {
System.out.println(prefix + this.name + " (" + this.getSize() + ")");
if (this.isDirectory) {
for (FileNode child : this.children) {
child.printNode(prefix + "| ");
}
}
}
public ArrayList<Integer> getDirectorySizesFlat(ArrayList<Integer> sizes) {
if (!this.isDirectory) {
return sizes;
}
sizes.add(this.getSize());
for (FileNode child : this.children) {
child.getDirectorySizesFlat(sizes);
}
return sizes;
}
}

View File

@@ -0,0 +1,116 @@
package com.felixalb.app;
import java.util.ArrayList;
public class FileSystemReader {
ArrayList<String[]> commands = new ArrayList<String[]>();
ArrayList<String> workingDirectory = new ArrayList<String>();
FileNode root = new FileNode("/", true);
public FileSystemReader(String[] lines) {
ArrayList<String> currentCommand = new ArrayList<String>();
for (String line : lines) {
if (line.startsWith("$") && currentCommand.size() > 0) {
commands.add(currentCommand.toArray(new String[0]));
currentCommand = new ArrayList<String>();
}
currentCommand.add(line);
}
if (currentCommand.size() > 0) {
commands.add(currentCommand.toArray(new String[0]));
}
System.out.println("Commands: " + commands.size());
for (String[] command : commands) {
this.parseCommand(command);
}
}
public void printState() {
System.out.println("Working directory: " + this.workingDirectory);
this.root.printNode("");
}
public FileNode getRoot() {
return this.root;
}
private FileNode getNode(String path, Boolean create) {
String[] pathParts = path.split("/");
FileNode currentNode = root;
for (String pathPart : pathParts) {
if (pathPart.length() == 0) {
continue;
}
ArrayList<FileNode> children = currentNode.getChildren();
FileNode child = null;
for (FileNode childCandidate : children) {
if (childCandidate.getName().equals(pathPart)) {
child = childCandidate;
break;
}
}
if (child == null) {
if (create) {
child = new FileNode(pathPart, false);
currentNode.appendChild(child);
} else {
return null;
}
}
currentNode = child;
}
return currentNode;
}
public ArrayList<String[]> getCommands() {
return commands;
}
public void parseCommand(String[] command) {
String[] commandParts = command[0].split(" ");
String commandName = commandParts[1];
// System.out.println("Command: " + commandName);
if (commandName.equals("cd")) {
String path = commandParts[2];
switch (path) {
case "/" :
workingDirectory = new ArrayList<String>();
break;
case ".." :
workingDirectory.remove(workingDirectory.size() - 1);
break;
default :
workingDirectory.add(path);
break;
}
// System.out.println("Working directory: /" + String.join("/", workingDirectory));
} else if (commandName.equals("ls")) {
FileNode dir = getNode(String.join("/", workingDirectory), false);
if (dir == null) {
System.out.println("ls: cannot access " + String.join("/", workingDirectory) + ": No such file or directory");
} else {
for (String line : command) {
String[] lineParts = line.split(" ");
if (lineParts[0].equals("$")) {
continue;
}
if (lineParts[0].equals("dir")) {
FileNode newDir = new FileNode(lineParts[1], true);
dir.appendChild(newDir);
} else {
FileNode newFile = new FileNode(lineParts[1], false);
newFile.setSize(Integer.parseInt(lineParts[0]));
dir.appendChild(newFile);
}
}
}
} else {
throw new RuntimeException("Unknown command: '" + commandName + "'");
}
}
}

View File

@@ -0,0 +1,20 @@
package com.felixalb.app;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
}