-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
258 additions
and
15 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip | ||
networkTimeout=10000 | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 74 additions & 1 deletion
75
src/main/java/net/leibi/adventofcode2022/day7/Day72022.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,75 @@ | ||
package net.leibi.adventofcode2022.day7;public class Day72022 { | ||
package net.leibi.adventofcode2022.day7; | ||
|
||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import net.leibi.helpers.InputHelper; | ||
|
||
public class Day72022 { | ||
|
||
static final Integer SIZELIMIT = 100000; | ||
|
||
public static Integer getSmallDirectoriesSize(String s) { | ||
var output = InputHelper.getRowListFromInput(s); | ||
|
||
var root = getDirectoryHierachy(output.subList(1, output.size()), null, "/"); | ||
|
||
return root.directoryList.stream().map(Directory::totalSize) | ||
.filter(directorySize -> directorySize < SIZELIMIT) | ||
.reduce(0, Integer::sum); | ||
} | ||
|
||
static Directory getDirectoryHierachy(List<String> output, Directory parentDirectory, | ||
String newDirectoryName) { | ||
if (newDirectoryName.equals("..")) { | ||
return parentDirectory; | ||
} | ||
var currentDirectory = new Directory(newDirectoryName); | ||
var addedDirectories = new ArrayList<Directory>(); | ||
for (int i = 0; i < output.size(); i++) { | ||
String outputRow = output.get(i); | ||
String[] splittedOutput = outputRow.split(" "); | ||
if (splittedOutput[0].equals("$") && splittedOutput[1].equals("cd") && !splittedOutput[2].equals("..")) { | ||
addedDirectories.add( | ||
getDirectoryHierachy(output.subList(i + 1, output.size()), currentDirectory, | ||
splittedOutput[2])); | ||
break; | ||
} else if (isFile(splittedOutput)) { | ||
currentDirectory.files.add(new File(splittedOutput[1], Integer.valueOf(splittedOutput[0]))); | ||
} else if (splittedOutput[0].equals("$") && splittedOutput[1].equals("cd") && splittedOutput[2].equals("..")){ | ||
currentDirectory.directoryList.addAll(addedDirectories); | ||
return currentDirectory; | ||
} | ||
} | ||
currentDirectory.directoryList.addAll(addedDirectories); | ||
return currentDirectory; | ||
} | ||
|
||
private static boolean isFile(String[] splittedOutput) { | ||
return splittedOutput[0].matches("\\d+"); | ||
} | ||
|
||
|
||
public record File(String name, Integer size) { | ||
|
||
} | ||
|
||
record Directory(String name, List<Directory> directoryList, List<File> files) { | ||
|
||
Directory(String name) { | ||
this(name, new ArrayList<>(), new ArrayList<>()); | ||
} | ||
|
||
public Integer totalSize() { | ||
return files.stream().map(File::size).reduce(0, Integer::sum) | ||
+ directoryList.stream().map(Directory::totalSize).reduce(0, Integer::sum); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Directory{" + | ||
"name='" + name + '\'' + | ||
'}'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,35 @@ | ||
package net.leibi.adventofcode2022.day7;public class Input { | ||
package net.leibi.adventofcode2022.day7; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class Input { | ||
|
||
public static final String small= """ | ||
$ 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 | ||
"""; | ||
|
||
} |
127 changes: 125 additions & 2 deletions
127
src/test/java/net/leibi/adventofcode2022/day7/Day72022Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,127 @@ | ||
import static org.junit.jupiter.api.Assertions.*; | ||
package net.leibi.adventofcode2022.day7; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import net.leibi.adventofcode2022.day7.Day72022.Directory; | ||
import net.leibi.adventofcode2022.day7.Day72022.File; | ||
import net.leibi.helpers.InputHelper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class Day72022Test { | ||
|
||
|
||
@Test | ||
void getDirectoryHierachy() { | ||
|
||
String input = """ | ||
$ ls | ||
dir a | ||
14848514 b.txt | ||
8504156 c.dat | ||
dir d | ||
"""; | ||
|
||
Directory directoryHierachy = Day72022.getDirectoryHierachy( | ||
InputHelper.getRowListFromInput(input), null, "/"); | ||
assertThat(directoryHierachy).isNotNull(); | ||
assertThat(directoryHierachy.directoryList()).isEmpty(); | ||
assertThat(directoryHierachy.files()).hasSize(2); | ||
|
||
|
||
|
||
} | ||
|
||
@Test | ||
void getDirectoryHierachyWithFilesInA() { | ||
|
||
String input = """ | ||
$ ls | ||
dir a | ||
14848514 b.txt | ||
8504156 c.dat | ||
dir d | ||
$ cd a | ||
$ ls | ||
dir e | ||
29116 f | ||
2557 g | ||
62596 h.lst | ||
"""; | ||
|
||
Directory directoryHierachy = Day72022.getDirectoryHierachy( | ||
InputHelper.getRowListFromInput(input), null, "/"); | ||
assertThat(directoryHierachy).isNotNull(); | ||
assertThat(directoryHierachy.directoryList()).hasSize(1); | ||
assertThat(directoryHierachy.directoryList().get(0).files()).hasSize(2); | ||
assertThat(directoryHierachy.files()).hasSize(2); | ||
|
||
} | ||
|
||
@Test | ||
void getDirectoryHierachyWithFilesInAAndE() { | ||
|
||
String input = """ | ||
$ 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 | ||
"""; | ||
|
||
Directory directoryHierachy = Day72022.getDirectoryHierachy( | ||
InputHelper.getRowListFromInput(input), null, "/"); | ||
assertThat(directoryHierachy).isNotNull(); | ||
assertThat(directoryHierachy.directoryList()).hasSize(1); | ||
assertThat(directoryHierachy.directoryList().get(0).name()).isEqualTo("a"); | ||
assertThat(directoryHierachy.directoryList().get(0).files()).hasSize(3); | ||
assertThat(directoryHierachy.directoryList().get(0).directoryList()).hasSize(1); | ||
assertThat(directoryHierachy.directoryList().get(0).directoryList().get(0).name()).isEqualTo("e"); | ||
assertThat(directoryHierachy.directoryList().get(0).directoryList().get(0).files()).hasSize(1); | ||
assertThat(directoryHierachy.files()).hasSize(2); | ||
} | ||
|
||
|
||
@Test | ||
void getDirectoryHierachySmall() { | ||
|
||
List<String> rowListFromInput = InputHelper.getRowListFromInput(Input.small); | ||
Directory directoryHierachy = Day72022.getDirectoryHierachy( | ||
rowListFromInput.subList(1,rowListFromInput.size()), null, "/"); | ||
assertThat(directoryHierachy).isNotNull(); | ||
assertThat(directoryHierachy.directoryList()).hasSize(1); | ||
assertThat(directoryHierachy.directoryList().get(0).name()).isEqualTo("a"); | ||
assertThat(directoryHierachy.directoryList().get(0).files()).hasSize(3); | ||
assertThat(directoryHierachy.directoryList().get(0).directoryList()).hasSize(1); | ||
assertThat(directoryHierachy.directoryList().get(0).directoryList().get(0).name()).isEqualTo("e"); | ||
assertThat(directoryHierachy.directoryList().get(0).directoryList().get(0).files()).hasSize(1); | ||
assertThat(directoryHierachy.files()).hasSize(2); | ||
} | ||
|
||
|
||
@Test | ||
void getSmallDirectoriesSize() { | ||
assertThat(Day72022.getSmallDirectoriesSize(Input.small)).isEqualTo(95437); | ||
} | ||
|
||
@Test | ||
void TestDirectoryTotalSize() { | ||
|
||
var dir = new Directory("a", new ArrayList<>(), new ArrayList<>()); | ||
dir.directoryList().add(new Directory("b", new ArrayList<>(), List.of(new File("f_a", 10)))); | ||
dir.files().add(new File("f_b", 100)); | ||
dir.files().add(new File("f_c", 5)); | ||
assertThat(dir.totalSize()).isEqualTo(115); | ||
} | ||
|
||
|
||
} |