Skip to content

Commit

Permalink
whatever
Browse files Browse the repository at this point in the history
  • Loading branch information
leibi committed Dec 4, 2023
1 parent e651be1 commit 6d94395
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 15 deletions.
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
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
18 changes: 14 additions & 4 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand All @@ -80,10 +80,10 @@ do
esac
done

APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit

APP_NAME="Gradle"
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
Expand Down Expand Up @@ -143,12 +143,16 @@ fi
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -205,6 +209,12 @@ set -- \
org.gradle.wrapper.GradleWrapperMain \
"$@"

# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
die "xargs is not available"
fi

# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
Expand Down
15 changes: 9 additions & 6 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@rem limitations under the License.
@rem

@if "%DEBUG%" == "" @echo off
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
Expand All @@ -25,7 +25,8 @@
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

Expand All @@ -40,7 +41,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Expand Down Expand Up @@ -75,13 +76,15 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar

:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
if %ERRORLEVEL% equ 0 goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%

:mainEnd
if "%OS%"=="Windows_NT" endlocal
Expand Down
75 changes: 74 additions & 1 deletion src/main/java/net/leibi/adventofcode2022/day7/Day72022.java
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 + '\'' +
'}';
}
}
}
35 changes: 34 additions & 1 deletion src/main/java/net/leibi/adventofcode2022/day7/Input.java
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 src/test/java/net/leibi/adventofcode2022/day7/Day72022Test.java
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);
}


}

0 comments on commit 6d94395

Please sign in to comment.