Skip to content

Commit

Permalink
Tighten filtering logic
Browse files Browse the repository at this point in the history
  • Loading branch information
szinn committed Apr 20, 2024
1 parent 5d343ed commit fdf68e5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
}}
>
<div class="grid" class:playing={1}>
{#each Array.from(Array(4).keys()) as row (row)}
{#each Array.from(Array(5).keys()) as row (row)}
{@const current = row === index}
<h2 class="visually-hidden">Row {row + 1}</h2>
<div class="row" class:current>
Expand Down
27 changes: 24 additions & 3 deletions src/routes/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,26 @@ export class Query {
];

// Remove not present letters and mark must be present
let misplaced = '';
for (let i = 0; i < guesses.length; i += 1) {
if (states[i] == '/') {
misplaced += guesses[i];
}
}
for (let i = 0; i < guesses.length; i += 1) {
const index = i % 5;

if (states[i] == 'x') {
for (let j = 0; j < 5; j += 1) {
candidates[j] = removeLetter(candidates[j], guesses[i]);
if (candidates[j] != guesses[i] && misplaced.indexOf(guesses[i]) == -1) {
candidates[j] = removeLetter(candidates[j], guesses[i]);
}
}
} else if (states[i] == '+') {
candidates[index] = guesses[i];
} else {
candidates[index] = removeLetter(candidates[index], guesses[i]);
}
console.log('Candidates', candidates);
}

const initialRegex = '[' + candidates.join('][') + ']';
Expand All @@ -84,7 +91,21 @@ export class Query {

for (let j = 0; j < 5; j += 1) {
if (states[i * 5 + j] == '/') {
const regex = new RegExp('.*' + guesses[i * 5 + j] + '.*');
let pattern = '';
for (let k = 0; k < 5; k += 1) {
if (k != j && states[i * 5 + k] != '+') {
pattern += '(^';
for (let l = 0; l < 5; l += 1) {
if (k == l) {
pattern += guesses[i * 5 + j];
} else {
pattern += '.';
}
}
pattern += ')|';
}
}
const regex = new RegExp(pattern.slice(0, -1));
for (let k = 0; k < prevMatches.length; k += 1) {
if (regex.test(prevMatches[k])) {
matches.push(prevMatches[k]);
Expand Down

0 comments on commit fdf68e5

Please sign in to comment.