Skip to content

Commit

Permalink
Improve performance of hidden-pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
melusc committed Jan 23, 2022
1 parent 4990b48 commit ca5252f
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions src/plugins/hidden-pairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,23 @@ const genericHiddenPairsSolver: VisitorFn = (structure, sudoku) => {
// This is a lot better than comparing arrays of indexes
const summary = new Map<number, bigint>();

// If there is `content`, the hidden pairs can't have
// that number anyway, so just keep track of them and ignore
// candidates that already exist as `content`
const foundContent = new Set<number>();

for (const [index, {content, candidates}] of structure.entries()) {
const pow = 1n << BigInt(index);

if (content === undefined) {
for (const candidate of candidates) {
summary.set(candidate, (summary.get(candidate) ?? 0n) | pow);
if (!foundContent.has(candidate)) {
summary.set(candidate, (summary.get(candidate) ?? 0n) | pow);
}
}
} else {
/*
This part fixes a bug:
[
{1,2,3}, // these are candidates
{1,2},
3, // this is filled in that cell
...
]
Webpack doesn't keep the same order as is exported from plugins.ts
(and the plugins shouldn't rely on it).
When remove-duplicates (now naked-pairs) doesn't run first, the scenarios above can occur
where hidden-pairs finds the only cell that has 3 in #candidates and then removes all others
(here cell at index 0), incorrectly resulting in two cells with 3
*/

summary.set(content, (summary.get(content) ?? 0n) | pow);
summary.delete(content);
foundContent.add(content);
}
}

Expand Down

0 comments on commit ca5252f

Please sign in to comment.