Advertisement
ConcealedReal

nytimes soduko auto solver

May 5th, 2024
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         NYTimes Sudoku Auto Solver
  3. // @namespace    http://tampermonkey.net/
  4. // @version      Beta
  5. // @description  Automatically solves the Sudoku for you on nytimes.com
  6. // @author       Splxff
  7. // @match        https://www.nytimes.com/puzzles/sudoku*
  8. // @icon         https://www.google.com/s2/favicons?sz=64&domain=nytimes.com
  9. // @grant        none
  10. // ==/UserScript==
  11.  
  12. ! function () {
  13.     function e() {
  14.         if (document.querySelector("[data-cell]")) {
  15.             var t = [];
  16.  
  17.             function o(e) {
  18.                 for (let t = 0; t < 9; t++) console.log(e[t].join(" "))
  19.             }
  20.  
  21.             function r(e, t, o, r) {
  22.                 for (let o = 0; o < 9; o++)
  23.                     if (e[t][o] === r) return !1;
  24.                 for (let t = 0; t < 9; t++)
  25.                     if (e[t][o] === r) return !1;
  26.                 const l = 3 * Math.floor(t / 3)
  27.                     , n = 3 * Math.floor(o / 3);
  28.                 for (let t = l; t < l + 3; t++)
  29.                     for (let o = n; o < n + 3; o++)
  30.                         if (e[t][o] === r) return !1;
  31.                 return !0
  32.             }
  33.             document.querySelectorAll("[data-cell]")
  34.                 .forEach(((e, o) => {
  35.                     const r = Math.floor(o / 9);
  36.                     t[r] ? t[r].length < 9 && t[r].push(parseInt(isNaN(e.getAttribute("aria-label")) ? 0 : e.getAttribute("aria-label"))) : t[r] = [parseInt(isNaN(e.getAttribute("aria-label")) ? 0 : e.getAttribute("aria-label"))], 80 === o && t.forEach(((e, t) => {
  37.                         for (; e.length < 9;) e.push(0)
  38.                     }))
  39.                 })), console.log("Sudoku before solving:"), o(t), console.log("\nSudoku after solving:"), ! function e(t) {
  40.                     for (let o = 0; o < 9; o++)
  41.                         for (let l = 0; l < 9; l++)
  42.                             if (0 === t[o][l]) {
  43.                                 for (let n = 1; n <= 9; n++)
  44.                                     if (r(t, o, l, n)) {
  45.                                         if (t[o][l] = n, e(t)) return !0;
  46.                                         t[o][l] = 0
  47.                                     } return !1
  48.                             } return !0
  49.                 }(t) ? console.log("No solution exists for the given Sudoku.") : o(t), Array.from(document.querySelectorAll("[data-cell]"))
  50.                 .forEach(((e, o) => {
  51.                     o--, setTimeout((() => {
  52.                         e.click();
  53.                         var r = t[Math.floor(o / 9)][o % 9]
  54.                             , l = Array.from(document.querySelectorAll("svg"))
  55.                             .filter((e => e.getAttribute("number") == r.toString()));
  56.                         l[l.length - 1].dispatchEvent(new MouseEvent("click", {
  57.                             bubbles: !0
  58.                         }))
  59.                     }), 1 * o)
  60.                 }))
  61.         } else setTimeout((() => {
  62.             e()
  63.         }), 2e3)
  64.     }
  65.     document.querySelector("[data-cell]") ? e() : setTimeout((() => {
  66.         e()
  67.     }), 2e3)
  68. }();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement