Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial refactoring in Day1 #43

Merged
merged 5 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions 2024/src/main/java/info/jab/aoc/day1/HistorianHysteria.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package info.jab.aoc.day1;

import com.putoet.resources.ResourceLines;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
Expand Down Expand Up @@ -34,6 +37,30 @@ private record ListSplitted(List<Integer> listLeft, List<Integer> listRight) {}
));
};

Function<List<String>, ListSplitted> splitInto2Lists3 = lines -> {
var result = lines.stream()
.collect(
// Supplier: Initialize a ListSplitted with two empty lists
() -> new ListSplitted(new ArrayList<Integer>(), new ArrayList<Integer>()),
// Accumulator: Split lines and add to respective lists
(ListSplitted accumulator, String line) -> {
String[] parts = SEPARATOR_PATTERN.split(line);
accumulator.listLeft().add(Integer.parseInt(parts[0]));
accumulator.listRight().add(Integer.parseInt(parts[1]));
},
// Combiner: Merge lists
(ListSplitted acc1, ListSplitted acc2) -> {
acc1.listLeft().addAll(acc2.listLeft());
acc1.listRight().addAll(acc2.listRight());
}
);
// Convert lists to sorted lists
return new ListSplitted(
result.listLeft().stream().sorted().toList(),
result.listRight().stream().sorted().toList()
);
};

Function<ListSplitted, Integer> calculateDistance = parameter -> {
return IntStream.range(0, parameter.listLeft().size())
.map(i -> {
Expand All @@ -59,11 +86,10 @@ private record ListSplitted(List<Integer> listLeft, List<Integer> listRight) {}
};

public Integer partOne(String fileName) {
return loadFle.andThen(splitInto2Lists).andThen(calculateDistance).apply(fileName);
return loadFle.andThen(splitInto2Lists3).andThen(calculateDistance).apply(fileName);
}

public Integer partTwo(String fileName) {
return loadFle.andThen(splitInto2Lists2).andThen(calculateOcurrences).apply(fileName);
return loadFle.andThen(splitInto2Lists3).andThen(calculateOcurrences).apply(fileName);
}

}
30 changes: 30 additions & 0 deletions 2024/src/test/java/info/jab/aoc/day1/AlternativeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package info.jab.aoc.day1;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;

public class AlternativeTest {

@State(Scope.Thread)
public static class St {
HistorianHysteria historianHysteria = new HistorianHysteria();
String fileName = "/day1/day1-input.txt";
}

@Benchmark
public void alternative1(St st) {
st.historianHysteria.loadFle.andThen(st.historianHysteria.splitInto2Lists).apply(st.fileName);
}

@Benchmark
public void alternative2(St st) {
st.historianHysteria.loadFle.andThen(st.historianHysteria.splitInto2Lists2).apply(st.fileName);
}

@Benchmark
public void alternative3(St st) {
st.historianHysteria.loadFle.andThen(st.historianHysteria.splitInto2Lists3).apply(st.fileName);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package info.jab.aoc.day1;

import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;

// WIP
// TODO Review this idea
@Disabled
@State(Scope.Thread)
@Threads(1)
public class AlternativesBenchmark2Test {

@Setup(Level.Invocation)
public void setupInvokation() throws Exception {
// executed before each invocation of the benchmark
}

@Setup(Level.Iteration)
public void setupIteration() throws Exception {
// executed before each invocation of the iteration
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
@Fork(warmups = 1, value = 1)
@Warmup(batchSize = -1, iterations = 3, time = 10, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(batchSize = -1, iterations = 10, time = 10, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void test() throws Exception {
HistorianHysteria historianHysteria = new HistorianHysteria();
String fileName = "/day1/day1-input.txt";
historianHysteria.loadFle.andThen(historianHysteria.splitInto2Lists).apply(fileName);
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
@Fork(warmups = 1, value = 1)
@Warmup(batchSize = -1, iterations = 3, time = 10, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(batchSize = -1, iterations = 10, time = 10, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void test2() throws Exception {
HistorianHysteria historianHysteria = new HistorianHysteria();
String fileName = "/day1/day1-input.txt";
historianHysteria.loadFle.andThen(historianHysteria.splitInto2Lists2).apply(fileName);
}

@Test
public void benchmark() throws Exception {
String[] argv = {};
org.openjdk.jmh.Main.main(argv);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package info.jab.aoc.day1;

import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.profile.JavaFlightRecorderProfiler;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openjdk.jmh.runner.options.TimeValue;

public class AlternativesBenchmarkTest {

@Test
public void should_show_best_alternatives() throws RunnerException {

Options options = new OptionsBuilder()
.include(AlternativeTest.class.getSimpleName())
.resultFormat(ResultFormatType.JSON)
.result("target/jmh-results.json")
//.verbosity(VerboseMode.EXTRA)
.mode(Mode.AverageTime)
.timeUnit(TimeUnit.MILLISECONDS)
.warmupTime(TimeValue.seconds(5))
.measurementTime(TimeValue.milliseconds(1))
.measurementIterations(10)
.threads(Runtime.getRuntime().availableProcessors())
.warmupIterations(1)
.shouldFailOnError(true)
.shouldDoGC(true)
.forks(3)
.jvmArgs("-Xmx6144m", "-Xms6144m")
//.addProfiler(StackProfiler.class)
//.addProfiler(GCProfiler.class)
//.addProfiler(LinuxPerfProfiler.class)
//.addProfiler(ClassloaderProfiler.class)
//.addProfiler(JavaFlightRecorderProfiler.class)
.build();

new Runner(options).run();
}

}
15 changes: 4 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ Advent of Code is an annual set of Christmas-themed computer programming challen
```bash
java -version
./mvnw clean verify
./mvnw -pl 2024 clean compile -am
./mvnw -pl 2024 clean verify -am
./mvnw -pl 2024 clean test -Dtest=Day24Test
./mvnw -pl 2024 clean test -Dtest=Day1Test
./mvnw -pl 2024 clean test -Dtest=AlternativesBenchmarkTest
./mvnw -pl 2024 clean dependency:tree
./mvnw -pl 2024 clean verify surefire-report:report -DshowSuccess=false
jwebserver -p 9000 -d "$(pwd)/2024/target/reports"
Expand Down Expand Up @@ -67,17 +69,8 @@ jwebserver -p 9000 -d "$(pwd)/2024/target/reports"

### Others

- https://jmh.morethan.io/
- https://adventofcode.com/
- https://adventofcode.com/2024
- https://adventofcode.com/2023
- https://adventofcode.com/2022
- https://adventofcode.com/2021
- https://adventofcode.com/2020
- https://adventofcode.com/2019
- https://adventofcode.com/2018
- https://adventofcode.com/2017
- https://adventofcode.com/2016
- https://adventofcode.com/2015
- https://github.com/jasonmuzzy/aoc-copilot
- https://www.reddit.com/r/adventofcode/
- https://www.reddit.com/r/adventofcode/?f=flair_name%3A%22Funny%22
Expand Down
54 changes: 48 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
<guava.version>31.1-jre</guava.version>
<commons-lang3.version>3.17.0</commons-lang3.version>

<jol.version>0.17</jol.version>
<junit-jupiter.version>5.11.3</junit-jupiter.version>
<junit-jupiter.version>5.11.4</junit-jupiter.version>
<assertj-core.version>3.26.3</assertj-core.version>

<checkstyle.skip>true</checkstyle.skip>
<jol.version>0.17</jol.version>
<jmh.version>1.37</jmh.version>
</properties>

<modules>
Expand Down Expand Up @@ -91,9 +91,10 @@

<!-- Testing -->
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>${jol.version}</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -110,6 +111,27 @@
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
</dependency>

<!-- JOL -->
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>${jol.version}</version>
</dependency>

<!-- JMH -->
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -159,6 +181,19 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>

<!-- JMH -->
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down Expand Up @@ -203,6 +238,13 @@
<compilerArgument>-Xlint:all</compilerArgument><!-- -Werror -->
<!-- <compilerArgument>- -enable-preview</compilerArgument> -->
<release>${java.version}</release>
<annotationProcessorPaths>
<path>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
Expand Down
Loading