Skip to content

Instantly share code, notes, and snippets.

@BohemianHacks
Created January 28, 2025 03:01
Show Gist options
  • Save BohemianHacks/0b5ef3966d1d3021db099c10904249e1 to your computer and use it in GitHub Desktop.
Save BohemianHacks/0b5ef3966d1d3021db099c10904249e1 to your computer and use it in GitHub Desktop.
const solveSudoku=(b)=>{const f=()=>{for(let r=0;r<9;r++)for(let c=0;c<9;c++)if(!b[r][c])return{r,c};return null};const i=(n,r,c)=>!b[r].includes(n)&&!b.map(r=>r[c]).includes(n)&&!b.slice(Math.floor(r/3)*3,Math.floor(r/3)*3+3).flatMap(r=>r.slice(Math.floor(c/3)*3,Math.floor(c/3)*3+3)).includes(n);const t=()=>{const{r,c}=f()||{};if(r===undefined)return!0;for(let n=1;n<=9;n++)if(i(n,r,c)){b[r][c]=n;if(t())return!0;b[r][c]=0;}return!1};return t()&&b};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment