import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
class SeedFinder {
static class SearchTask implements Callable<Long> {
private final char[] goal;
private final long start, step;
public SearchTask
(final String goal,
final long offset,
final long step
) { final char[] goalAsArray = goal.toCharArray();
this.goal = new char[goalAsArray.length + 1];
System.
arraycopy(goalAsArray,
0,
this.
goal,
0, goalAsArray.
length); this.
start = Long.
MIN_VALUE + offset
; this.step = step;
}
@Override
final long LIMIT
= Long.
MAX_VALUE - this.
step; int position, rnd;
long seed = this.start;
while ((Thread.
interrupted() == false) && (seed
< LIMIT
)) { random.setSeed(seed);
position = 0;
rnd = random.nextInt(27);
while (((rnd == 0) && (this.goal[position] == 0))
|| ((char) ('`' + rnd) == this.goal[position])) {
++position;
if (position == this.goal.length) {
return seed;
}
rnd = random.nextInt(27);
}
seed += this.step;
}
}
}
public static void main
(String[] args
) { final String GOAL
= "hello".
toLowerCase(); final int NUM_CORES
= Runtime.
getRuntime().
availableProcessors();
final ArrayList<SearchTask> tasks = new ArrayList<>(NUM_CORES);
for (int i = 0; i < NUM_CORES; ++i) {
tasks.add(new SearchTask(GOAL, i, NUM_CORES));
}
final ExecutorService executor = Executors.newFixedThreadPool(NUM_CORES, new ThreadFactory() {
@Override
result.
setPriority(Thread.
MIN_PRIORITY); result.setDaemon(false);
return result;
}
});
try {
final Long result
= executor.
invokeAny(tasks
); System.
out.
println("Seed for \"" + GOAL
+ "\" found: " + result
); System.
err.
println("Calculation failed: " + ex
); } finally {
executor.shutdownNow();
}
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheUxpc3Q7CmltcG9ydCBqYXZhLnV0aWwuUmFuZG9tOwppbXBvcnQgamF2YS51dGlsLmNvbmN1cnJlbnQuQ2FsbGFibGU7CmltcG9ydCBqYXZhLnV0aWwuY29uY3VycmVudC5FeGVjdXRvclNlcnZpY2U7CmltcG9ydCBqYXZhLnV0aWwuY29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