day07 part 1
This commit is contained in:
parent
a4079ac7dc
commit
57150f3fde
|
@ -0,0 +1,7 @@
|
|||
Run with command:
|
||||
|
||||
```
|
||||
mvn package && java -cp target/aoc07-1.0-SNAPSHOT.jar com.felixalb.app.App input.txt
|
||||
```
|
||||
|
||||
Code is located in `src/main/java/com/felixalb/app/`
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,23 @@
|
|||
$ cd /
|
||||
$ ls
|
||||
dir a
|
||||
14848514 b.txt
|
||||
8504156 c.dat
|
||||
dir d
|
||||
$ cd a
|
||||
$ ls
|
||||
dir e
|
||||
29116 f
|
||||
2557 g
|
||||
62596 h.lst
|
||||
$ cd e
|
||||
$ ls
|
||||
584 i
|
||||
$ cd ..
|
||||
$ cd ..
|
||||
$ cd d
|
||||
$ ls
|
||||
4060174 j
|
||||
8033020 d.log
|
||||
5626152 d.ext
|
||||
7214296 k
|
|
@ -0,0 +1,75 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.felixalb.app</groupId>
|
||||
<artifactId>aoc07</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<name>aoc07</name>
|
||||
<!-- FIXME change it to the project's website -->
|
||||
<url>http://www.example.com</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.7</maven.compiler.source>
|
||||
<maven.compiler.target>1.7</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
|
||||
<plugins>
|
||||
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
|
||||
<plugin>
|
||||
<artifactId>maven-clean-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</plugin>
|
||||
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-install-plugin</artifactId>
|
||||
<version>2.5.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>2.8.2</version>
|
||||
</plugin>
|
||||
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
|
||||
<plugin>
|
||||
<artifactId>maven-site-plugin</artifactId>
|
||||
<version>3.7.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-project-info-reports-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
</project>
|
|
@ -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);
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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 + "'");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue