Day 7 Structured Concurrency
This commit is contained in:
21
pom.xml
21
pom.xml
@@ -14,6 +14,19 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<scmDeveloperConnectionProp />
|
||||
</properties>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>25</source>
|
||||
<target>25</target>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<distributionManagement>
|
||||
<snapshotRepository>
|
||||
@@ -32,4 +45,12 @@
|
||||
<url>https://gitea.77zzcx7.de/MK13/advent_of_code_2025</url>
|
||||
</scm>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.vavr</groupId>
|
||||
<artifactId>vavr</artifactId>
|
||||
<version>0.10.7</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
125
src/main/java/de/advent_of_code_2025/day7/ImmutableManifold.java
Normal file
125
src/main/java/de/advent_of_code_2025/day7/ImmutableManifold.java
Normal file
@@ -0,0 +1,125 @@
|
||||
package de.advent_of_code_2025.day7;
|
||||
|
||||
import io.vavr.collection.List;
|
||||
|
||||
import java.util.concurrent.StructuredTaskScope;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
public class ImmutableManifold {
|
||||
private List<List<Character>> rowCells;
|
||||
private StructuredTaskScope scope;
|
||||
|
||||
public ImmutableManifold(java.util.List<String> lines) {
|
||||
this.rowCells = List.empty();
|
||||
|
||||
for (String line : lines) {
|
||||
this.rowCells = this.rowCells.append(List.ofAll(line.toCharArray()));
|
||||
}
|
||||
|
||||
for (int i = 0; i < this.rowCells.get(0).size(); i++) {
|
||||
if(this.rowCells.get(0).get(i) == 'S') {
|
||||
this.rowCells = update(this.rowCells, 1, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static List<List<Character>> update(List<List<Character>> current, int rowIndex, int columnIndex) {
|
||||
return current.update(rowIndex, current.get(rowIndex).update(columnIndex, '|'));
|
||||
}
|
||||
|
||||
static AtomicLong TASK_COUNTER = new AtomicLong(0);
|
||||
|
||||
public long beamTimeline() {
|
||||
final AtomicLong counter = new AtomicLong(0);
|
||||
|
||||
try(var scope = StructuredTaskScope.open()) {
|
||||
for(int i = 0; i < this.rowCells.get(2).size(); i++) {
|
||||
if(this.rowCells.get(2).get(i) == '^') {
|
||||
List<List<Character>> taskState = update(update(this.rowCells, 2, i - 1), 3, i - 1);
|
||||
scope.fork(() -> travel(counter, taskState, 3));
|
||||
if(TASK_COUNTER.incrementAndGet() % 1_000_000 == 0) {
|
||||
System.out.println(TASK_COUNTER.get());
|
||||
}
|
||||
|
||||
List<List<Character>> taskState2 = update(update(this.rowCells, 2, i + 1), 3, i + 1);
|
||||
scope.fork(() -> travel(counter, taskState2, 3));
|
||||
if(TASK_COUNTER.incrementAndGet() % 1_000_000 == 0) {
|
||||
System.out.println(TASK_COUNTER.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
scope.join();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return counter.get();
|
||||
}
|
||||
|
||||
private static void travel(AtomicLong counter, List<List<Character>> rowCells, int rowCounter) {
|
||||
if(rowCells.size() - 1 == rowCounter) {
|
||||
// print(rowCells);
|
||||
counter.incrementAndGet();
|
||||
return;
|
||||
}
|
||||
|
||||
try(var scope = StructuredTaskScope.open()) {
|
||||
// for (int i = rowCounter; i < rowCells.get(rowCounter).size(); i++) {
|
||||
for (int j = 0; j < rowCells.get(rowCounter).size(); j++) {
|
||||
if (rowCells.get(rowCounter).get(j) == '|' && rowCells.get(rowCounter + 1).get(j) == '^') {
|
||||
List<List<Character>> taskState = update(update(rowCells, rowCounter + 1, j - 1), rowCounter + 2, j - 1);
|
||||
scope.fork(() -> travel(counter, taskState, rowCounter + 1));
|
||||
|
||||
if(TASK_COUNTER.incrementAndGet() % 1_000_000 == 0) {
|
||||
System.out.println(TASK_COUNTER.get());
|
||||
}
|
||||
|
||||
List<List<Character>> taskState2 = update(update(rowCells, rowCounter + 1, j + 1), rowCounter + 2, j + 1);
|
||||
scope.fork(() -> travel(counter, taskState2, rowCounter + 1));
|
||||
if(TASK_COUNTER.incrementAndGet() % 1_000_000 == 0) {
|
||||
System.out.println(TASK_COUNTER.get());
|
||||
}
|
||||
}
|
||||
else if (rowCells.get(rowCounter).get(j) == '|') {
|
||||
List<List<Character>> taskState = update(rowCells, rowCounter + 1, j);
|
||||
scope.fork(() -> travel(counter, taskState, rowCounter + 1));
|
||||
if(TASK_COUNTER.incrementAndGet() % 1_000_000 == 0) {
|
||||
System.out.println(TASK_COUNTER.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
// }
|
||||
|
||||
scope.join();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
if(TASK_COUNTER.decrementAndGet() % 100_000 == 0) {
|
||||
System.out.println(TASK_COUNTER.get());
|
||||
}
|
||||
}
|
||||
|
||||
static AtomicInteger C = new AtomicInteger(0);
|
||||
|
||||
private static final void print(List<List<Character>> rowCells) {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append(C.incrementAndGet());
|
||||
sb.append("\r\n");
|
||||
rowCells.forEach(rc -> {
|
||||
rc.forEach(sb::append);
|
||||
sb.append("\r\n");
|
||||
});
|
||||
sb.append("----------------------");
|
||||
|
||||
System.out.println(sb);
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
14
src/main/java/de/advent_of_code_2025/day7/Main2.java
Normal file
14
src/main/java/de/advent_of_code_2025/day7/Main2.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package de.advent_of_code_2025.day7;
|
||||
|
||||
import de.advent_of_code_2025.util.InputReader;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Main2 {
|
||||
public static void main(String[] args) throws Throwable {
|
||||
final List<String> lines = InputReader.read(args);
|
||||
final ImmutableManifold manifold = new ImmutableManifold(lines);
|
||||
|
||||
System.out.println(manifold.beamTimeline());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user