optimize with sort

This commit is contained in:
Adrien MALINGREY 2023-11-25 19:55:59 +01:00
parent ad414d0da8
commit 1365ef65dd
2 changed files with 36 additions and 19 deletions

View File

@ -36,6 +36,10 @@ table input:not([disabled]) {
background: transparent !important;
}
table input[disabled] {
background-position: center !important;
}
tr:nth-child(3n+1) td input {
border-top-width: 3px !important;
}

View File

@ -176,13 +176,43 @@ function oninput() {
function checkBox(box) {
checkCandidates(box.andNeighbourhood)
checkDuplicates(box)
if (box.value) {
for (let [areaName, neighbours] of Object.entries({
région: regions[box.regionId],
ligne: rows[box.rowId],
colonne: columns[box.columnId],
}))
for (neighbour of neighbours)
if (box != neighbour && box.value == neighbour.value) {
for (neighbour of [box, neighbour]) {
neighbour.setCustomValidity(`Il y a un autre ${box.value} dans cette ${areaName}.`)
neighbour.classList.add("is-invalid")
}
}
}
checkSuccess()
}
function checkBoxes() {
checkCandidates(boxes)
boxes.forEach(checkDuplicates)
for (let [areaName, areas] of Object.entries({
région: regions,
ligne: rows,
colonne: columns,
}))
for (area of areas)
area.sort((box, neighbour) => {
if (box.value && box.value == neighbour.value) {
for (neighbour of [box, neighbour]) {
neighbour.setCustomValidity(`Il y a un autre ${box.value} dans cette ${areaName}.`)
neighbour.classList.add("is-invalid")
}
}
})
checkSuccess()
}
@ -198,23 +228,6 @@ function checkCandidates(area) {
})
}
function checkDuplicates(box) {
if (box.value) {
for (let [area, neighbours] of Object.entries({
région: regions[box.regionId],
ligne: rows[box.rowId],
colonne: columns[box.columnId],
}))
for (neighbour of neighbours)
if (box != neighbour && box.value == neighbour.value) {
for (neighbour of [box, neighbour]) {
neighbour.setCustomValidity(`Il y a un autre ${box.value} dans cette ${area}.`)
neighbour.classList.add("is-invalid")
}
}
}
}
function checkSuccess() {
if (sudokuForm.checkValidity()) { // Correct grid
if (boxes.filter(box => box.value == "").length == 0) {