///usr/bin/env jbang "$0" "$@" ; exit $? //JAVA 25 //RUNTIME_OPTIONS --enable-native-access=ALL-UNNAMED --add-opens=java.base/java.nio=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/sun.nio.ch=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED //DEPS org.neo4j:neo4j:2025.09.0 import org.neo4j.dbms.api.DatabaseManagementServiceBuilder; void main() throws IOException, InterruptedException { var height = 30; var width = 30; var sleep = Duration.ofMillis(500); var random = ThreadLocalRandom.current(); var numAlive = (int) (height * width * 0.1); var cells = random.ints(numAlive * 2, 0, height).boxed().gather(Gatherers.windowSliding(2)).toList(); try (var dbms = new DatabaseManagementServiceBuilder(Files.createTempDirectory("neo4j")).build()) { var db = dbms.database("neo4j"); db.executeTransactionally(""" UNWIND range(0,$height-1) AS row WITH row UNWIND range(0,$width-1) AS column WITH row, column CREATE (cell:Cell {coordinate: Point({x:column,y:row}), alive:false}) WITH row, column, cell UNWIND [[row-1, column-1],[row-1, column],[row-1, column+1],[row, column-1]] AS neighbor MATCH (other:Cell {coordinate: Point({x:neighbor[1],y:neighbor[0]})}) MERGE (other)-[:NEIGHBOUR_OF]->(cell) """, Map.of("height", height, "width", width)); db.executeTransactionally(""" UNWIND $cells AS cell MATCH (c:Cell {coordinate: Point({x:cell[0],y:cell[1]})}) SET c.alive = true """, Map.of("cells", cells)); var previous = ""; while (true) { var result = db.executeTransactionally(""" MATCH (cell:Cell) OPTIONAL MATCH (cell)-[:NEIGHBOUR_OF]-(:Cell {alive: true }) WITH cell, COUNT(*) AS liveNeighbours SET cell.alive = CASE liveNeighbours WHEN <= 1 THEN FALSE WHEN >= 4 THEN FALSE WHEN = 3 THEN TRUE ELSE cell.alive END WITH cell ORDER BY cell.coordinate.y ASC, cell.coordinate.x ASC WITH collect(cell) AS cells RETURN reduce(res = "", cell IN cells | res + CASE cell.coordinate.x = 0 WHEN true THEN "\\n" ELSE "" END + CASE cell.alive WHEN true THEN '\u001b[1;38;5;0;48;5;253m ■ \u001b[0m' ELSE '\u001b[1;38;5;214;48;5;253m ■ \u001b[0m' END) AS result """, Map.of(), rows -> rows.next().get("result").toString()); IO.print("\033[H\033[2J"); IO.println(result); System.out.flush(); Thread.sleep(sleep); if (result.equals(previous)) { break; } previous = result; } } }