Skip to content

Instantly share code, notes, and snippets.

@geelen
Last active June 11, 2020 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geelen/770d908cdc8717a612403961dc88a076 to your computer and use it in GitHub Desktop.
Save geelen/770d908cdc8717a612403961dc88a076 to your computer and use it in GitHub Desktop.
Cracking the Cryptic webapp helper

Cracking the Cryptic sudoku webapp helper

This just detects if you've made a clear error as you're solving something (only looks at the basic sudoku rules, nothing fancy):

image

To install, just open your developer console (⌘⇧I or Cmd Shift I, then select console) and paste in the source code below:

const grid = $('.sudoku-grid')
const cells = grid.querySelectorAll('.sudoku-cell__inner')
function check() {
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
const a = i * 9 + j
cells[a].style.boxShadow = ''
const val_a = cells[a].querySelector('.sudoku-cell__value')
cells[a].__value = val_a ? val_a.innerText : null
}
}
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
const a = i * 9 + j
const val_a = cells[a].__value
if (!val_a) continue
for (let k = i; k < 9; k++) {
for (let l = 0; l < 9; l++) {
if (k === i && l < j) continue
const same_row = i === k
const same_col = j === l
if (same_row && same_col) continue
const same_region =
Math.floor(i / 3) === Math.floor(k / 3) &&
Math.floor(j / 3) === Math.floor(l / 3)
if (!(same_row || same_col || same_region)) continue
const b = k * 9 + l
const same_value = val_a === cells[b].__value
if (same_value) {
console.log(
`Cell ${i + 1},${j + 1} and ${k + 1},${l + 1} are both ${val_a}`
)
cells[a].style.boxShadow = 'inset 0 0 4px 4px red'
cells[b].style.boxShadow = 'inset 0 0 4px 4px red'
}
}
}
}
}
}
const observer = new MutationObserver(check)
cells.forEach(cell =>
observer.observe(cell, {
childList: true,
subtree: true,
characterData: true
})
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment