From 1376d9abdfc1c8a83191a7c065353f08973023cf Mon Sep 17 00:00:00 2001 From: Hexicube Date: Sat, 27 Mar 2021 10:24:15 +0000 Subject: [PATCH 1/2] Pre-compute values to speed up first search --- src/enchcracker/SimpleRandom.java | 10 ++ .../cracker/JavaSingleSeedCracker.java | 109 ++++++++++++------ 2 files changed, 83 insertions(+), 36 deletions(-) diff --git a/src/enchcracker/SimpleRandom.java b/src/enchcracker/SimpleRandom.java index 60d6dac..09259d0 100644 --- a/src/enchcracker/SimpleRandom.java +++ b/src/enchcracker/SimpleRandom.java @@ -15,6 +15,16 @@ private int next() { return (int)(seed >>> 17); } + // always used + public int next8() { + return (int)((8 * (long)next()) >> 31); + } + + // specifically for 15 shelves + public int next8and16() { + return (int)((8 * (long)next()) >> 31) + (int)((16 * (long)next()) >> 31); + } + public int nextInt(int bound) { int r = next(); int m = bound - 1; diff --git a/src/enchcracker/cracker/JavaSingleSeedCracker.java b/src/enchcracker/cracker/JavaSingleSeedCracker.java index cdcf2d8..7783f85 100644 --- a/src/enchcracker/cracker/JavaSingleSeedCracker.java +++ b/src/enchcracker/cracker/JavaSingleSeedCracker.java @@ -64,6 +64,19 @@ public void firstInput(int bookshelves, int slot1, int slot2, int slot3) { final int secondEarly = slot2 * 3 / 2; final int secondSubOne = slot2 - 1; + final int test1 = firstEarly - halfShelves; + final int test3 = secondEarly - halfShelves; + final boolean[] test2 = new boolean[shelvesPlusOne + 7]; + final boolean[] test4 = new boolean[shelvesPlusOne + 7]; + final boolean[] test5 = new boolean[shelvesPlusOne + 7]; + for (int a = 0; a < shelvesPlusOne + 7; a++) { + int v = (a + halfShelves) / 3; + test2[a] = ((v < 1 && slot1 != 1) || v != slot1); + v = (a + halfShelves) * 2 / 3; + test4[a] = v != secondSubOne; + test5[a] = Math.max(a + halfShelves, twoShelves) == slot3; + } + seedsSearched.set(0); possibleSeeds.clear(); @@ -71,45 +84,69 @@ public void firstInput(int bookshelves, int slot1, int slot2, int slot3) { long startTime = System.nanoTime(); for (int i = 0; i < threadCount; i++) { - Thread t = new Thread(() -> { - final int[] myList = new int[1000000]; - int pos = 0; - final SimpleRandom myRNG = new SimpleRandom(); - - while (true) { - if (abortRequested.get()) return; - - int curSeed = seed.get(); - final int last = curSeed + blockSize; - if (last < curSeed) break; // overflow - if (seed.compareAndSet(curSeed, curSeed + blockSize)) { - for (; curSeed < last; curSeed++) { - myRNG.setSeed(curSeed); - - int ench1r1 = myRNG.nextInt(8) + halfShelves; - if (ench1r1 > firstEarly) continue; - int ench1 = (ench1r1 + myRNG.nextInt(shelvesPlusOne)) / 3; - if (ench1 < 1) { if (slot1 != 1) continue; } - if (ench1 != slot1) continue; - - int ench2r1 = myRNG.nextInt(8) + halfShelves; - if (ench2r1 > secondEarly) continue; - int ench2 = (ench2r1 + myRNG.nextInt(shelvesPlusOne)) * 2 / 3; - if (ench2 != secondSubOne) continue; - - int ench3 = (myRNG.nextInt(8) + halfShelves + myRNG.nextInt(shelvesPlusOne)); - if (Math.max(ench3, twoShelves) != slot3) continue; - - myList[pos++] = curSeed; - if (pos == myList.length) { - synchronized(possibleSeeds) { possibleSeeds.addAll(myList, myList.length); } - pos = 0; + Thread t; + if (shelvesPlusOne == 16) { + t = new Thread(() -> { + final int[] myList = new int[1000000]; + int pos = 0; + final SimpleRandom myRNG = new SimpleRandom(); + + while (true) { + if (abortRequested.get()) return; + + int curSeed = seed.get(); + final int last = curSeed + blockSize; + if (last < curSeed) break; // overflow + if (seed.compareAndSet(curSeed, curSeed + blockSize)) { + for (; curSeed < last; curSeed++) { + myRNG.setSeed(curSeed); + if (test2[myRNG.next8and16()]) continue; + if (test4[myRNG.next8and16()]) continue; + if (test5[myRNG.next8and16()]) { + myList[pos++] = curSeed; + if (pos == myList.length) { + synchronized(possibleSeeds) { possibleSeeds.addAll(myList, myList.length); } + pos = 0; + } + } } } } - } - synchronized(possibleSeeds) { possibleSeeds.addAll(myList, pos); } - }); + synchronized(possibleSeeds) { possibleSeeds.addAll(myList, pos); } + }); + } + else { + t = new Thread(() -> { + final int[] myList = new int[1000000]; + int pos = 0, v; + final SimpleRandom myRNG = new SimpleRandom(); + + while (true) { + if (abortRequested.get()) return; + + int curSeed = seed.get(); + final int last = curSeed + blockSize; + if (last < curSeed) break; // overflow + if (seed.compareAndSet(curSeed, curSeed + blockSize)) { + for (; curSeed < last; curSeed++) { + myRNG.setSeed(curSeed); + v = myRNG.next8(); + if (v > test1 || test2[v + myRNG.nextInt(shelvesPlusOne)]) continue; + v = myRNG.next8(); + if (v > test3 || test4[v + myRNG.nextInt(shelvesPlusOne)]) continue; + if (test5[myRNG.next8() + myRNG.nextInt(shelvesPlusOne)]) { + myList[pos++] = curSeed; + if (pos == myList.length) { + synchronized(possibleSeeds) { possibleSeeds.addAll(myList, myList.length); } + pos = 0; + } + } + } + } + } + synchronized(possibleSeeds) { possibleSeeds.addAll(myList, pos); } + }); + } threads.add(t); t.start(); } From dfad64d544941c1004fdaa87bc6934bd73f89bc3 Mon Sep 17 00:00:00 2001 From: Hexicube Date: Sat, 27 Mar 2021 11:59:23 +0000 Subject: [PATCH 2/2] Improvement to next8 and next8plus16 --- src/enchcracker/SimpleRandom.java | 6 +++--- src/enchcracker/cracker/JavaSingleSeedCracker.java | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/enchcracker/SimpleRandom.java b/src/enchcracker/SimpleRandom.java index 09259d0..23a52ff 100644 --- a/src/enchcracker/SimpleRandom.java +++ b/src/enchcracker/SimpleRandom.java @@ -17,12 +17,12 @@ private int next() { // always used public int next8() { - return (int)((8 * (long)next()) >> 31); + return (int)((long)next() >>> 28); } // specifically for 15 shelves - public int next8and16() { - return (int)((8 * (long)next()) >> 31) + (int)((16 * (long)next()) >> 31); + public int next8plus16() { + return (int)((long)next() >>> 28) + (int)((long)next() >>> 27); } public int nextInt(int bound) { diff --git a/src/enchcracker/cracker/JavaSingleSeedCracker.java b/src/enchcracker/cracker/JavaSingleSeedCracker.java index 7783f85..085cbfc 100644 --- a/src/enchcracker/cracker/JavaSingleSeedCracker.java +++ b/src/enchcracker/cracker/JavaSingleSeedCracker.java @@ -100,9 +100,9 @@ public void firstInput(int bookshelves, int slot1, int slot2, int slot3) { if (seed.compareAndSet(curSeed, curSeed + blockSize)) { for (; curSeed < last; curSeed++) { myRNG.setSeed(curSeed); - if (test2[myRNG.next8and16()]) continue; - if (test4[myRNG.next8and16()]) continue; - if (test5[myRNG.next8and16()]) { + if (test2[myRNG.next8plus16()]) continue; + if (test4[myRNG.next8plus16()]) continue; + if (test5[myRNG.next8plus16()]) { myList[pos++] = curSeed; if (pos == myList.length) { synchronized(possibleSeeds) { possibleSeeds.addAll(myList, myList.length); }