From ebddbc1de5f3485512353be22e5a14a1c4d8ba7f Mon Sep 17 00:00:00 2001 From: MK13 Date: Thu, 11 Dec 2025 09:36:49 +0100 Subject: [PATCH] Day 7 Structured Concurrency --- pom.xml | 21 +++ .../day7/ImmutableManifold.java | 125 ++++++++++++++++++ .../de/advent_of_code_2025/day7/Main2.java | 14 ++ 3 files changed, 160 insertions(+) create mode 100644 src/main/java/de/advent_of_code_2025/day7/ImmutableManifold.java create mode 100644 src/main/java/de/advent_of_code_2025/day7/Main2.java diff --git a/pom.xml b/pom.xml index 3b7243e..4b4f19c 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,19 @@ UTF-8 + + + + org.apache.maven.plugins + maven-compiler-plugin + + 25 + 25 + --enable-preview + + + + @@ -32,4 +45,12 @@ https://gitea.77zzcx7.de/MK13/advent_of_code_2025 + + + io.vavr + vavr + 0.10.7 + + + \ No newline at end of file diff --git a/src/main/java/de/advent_of_code_2025/day7/ImmutableManifold.java b/src/main/java/de/advent_of_code_2025/day7/ImmutableManifold.java new file mode 100644 index 0000000..d53df67 --- /dev/null +++ b/src/main/java/de/advent_of_code_2025/day7/ImmutableManifold.java @@ -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> rowCells; + private StructuredTaskScope scope; + + public ImmutableManifold(java.util.List 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> update(List> 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> 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> 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> 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> 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> 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> 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> 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); + } + } +} diff --git a/src/main/java/de/advent_of_code_2025/day7/Main2.java b/src/main/java/de/advent_of_code_2025/day7/Main2.java new file mode 100644 index 0000000..582b571 --- /dev/null +++ b/src/main/java/de/advent_of_code_2025/day7/Main2.java @@ -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 lines = InputReader.read(args); + final ImmutableManifold manifold = new ImmutableManifold(lines); + + System.out.println(manifold.beamTimeline()); + } +}