diff --git a/src/main/java/puzzle/Main.java b/src/main/java/puzzle/Main.java index d2c628f..e50f1ce 100644 --- a/src/main/java/puzzle/Main.java +++ b/src/main/java/puzzle/Main.java @@ -389,7 +389,9 @@ public class Main { var mask = swe.generateMask(opts.clueSize, opts.pop, opts.gens, opts.offspring); if (mask == null) return null; val multiThreaded = Thread.currentThread().getName().contains("pool"); - var filled = fillMask(rng, extractSlots(mask, dict.index()), mask.toGrid(), multiThreaded); + var slots = extractSlots(mask, dict.index()); + val slotInfo = scoreSlots(new int[slots.length], slots); + var filled = fillMask(rng, slotInfo, mask.toGrid(), multiThreaded); TOTAL_NODES.addAndGet(filled.stats().nodes); TOTAL_BACKTRACKS.addAndGet(filled.stats().backtracks); diff --git a/src/main/java/puzzle/SwedishGenerator.java b/src/main/java/puzzle/SwedishGenerator.java index c638af3..139cf53 100644 --- a/src/main/java/puzzle/SwedishGenerator.java +++ b/src/main/java/puzzle/SwedishGenerator.java @@ -78,9 +78,9 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) { @AllArgsConstructor static class Pick { - Slot slot; - int[] indices; - int count; + Slotinfo slot; + int[] indices; + int count; } // 0b11 @@ -335,6 +335,10 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) { } } + static record Slotinfo(int key, long lo, long hi, int score, DictEntry entry) { + + } + static record Slot(int key, long lo, long hi, DictEntry entry) { static final int BIT_FOR_DIR = 2; @@ -874,8 +878,9 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) { return count; } - static void scoreSlots(int[] slotScores, Slot[] slots) { - val count = new byte[SIZE]; + static Slotinfo[] scoreSlots(int[] slotScores, Slot[] slots) { + val count = new byte[SIZE]; + Slotinfo[] slotInfo = new Slotinfo[slots.length]; for (var s : slots) { for (long b = s.lo; b != X; b &= b - 1) count[numberOfTrailingZeros(b)]++; for (long b = s.hi; b != X; b &= b - 1) count[64 | numberOfTrailingZeros(b)]++; @@ -883,27 +888,23 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) { for (int i = 0; i < slots.length; i++) { var slot = slots[i]; slotScores[i] = slotScore(count, slot.lo, slot.hi); + slotInfo[i] = new Slotinfo(slot.key, slot.lo, slot.hi, slotScores[i], slot.entry); } - + return slotInfo; } - public static FillResult fillMask(final Rng rng,final Slot[] slots,final Grid mask, final boolean multiThreaded) { + public static FillResult fillMask(final Rng rng, final Slotinfo[] slots, + final Grid grid, + final boolean multiThreaded) { val NO_LOG = (!Main.VERBOSE || multiThreaded); - val grid = mask; val used = new long[2048]; val assigned = new long[CLUE_INDEX_MAX_SIZE]; val bitset = new long[2500]; val g = grid.g; - - val TOTAL = slots.length; - val slotScores = new int[TOTAL]; - - scoreSlots(slotScores, slots); - - val t0 = System.currentTimeMillis(); - + val TOTAL = slots.length; + val t0 = System.currentTimeMillis(); + val CARRIER = new Pick(null, null, 0); class Solver { - private final Pick CARRIER = new Pick(null, null, 0); long nodes; long backtracks; int lastMRV; @@ -963,7 +964,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) { return true; } void chooseMRV() { - Slot best = null; + Slotinfo best = null; for (int i = 0, count, count2 = -1, bestScore = -1, n = TOTAL; i < n; i++) { var s = slots[i]; if (assigned[s.key] != X) continue; @@ -977,9 +978,9 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) { } if (best == null || count < count2 - || (count == count2 && slotScores[i] > bestScore)) { + || (count == count2 && s.score > bestScore)) { best = s; - bestScore = slotScores[i]; + bestScore = s.score; count2 = count; if (count <= 1) break; } diff --git a/src/test/java/puzzle/MainTest.java b/src/test/java/puzzle/MainTest.java index 4c19de7..dd5f2c1 100644 --- a/src/test/java/puzzle/MainTest.java +++ b/src/test/java/puzzle/MainTest.java @@ -180,7 +180,9 @@ public class MainTest { 128L, 422762372923520L, 192L); - var filled = fillMask(rng, extractSlots(mask, dict.index()), mask.toGrid(), false); + var slots = extractSlots(mask, dict.index()); + val slotInfo = scoreSlots(new int[slots.length], slots); + var filled = fillMask(rng, slotInfo, mask.toGrid(), false); Assertions.assertTrue(filled.ok(), "Puzzle generation failed (not ok)"); Assertions.assertEquals(18, filled.wordCount(0), "Number of assigned words changed"); Assertions.assertEquals("SLEDE", Lemma.asWord(filled.clueMap()[282]));