Skip to content

Instantly share code, notes, and snippets.

@greymd
Last active July 20, 2022 11:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greymd/981d51a2d6dc4d99ea0c4be4be43e69c to your computer and use it in GitHub Desktop.
Save greymd/981d51a2d6dc4d99ea0c4be4be43e69c to your computer and use it in GitHub Desktop.
Toriumi-san's sudoku solver
## original:
## https://twitter.com/hid_tori/status/1221415626465349635
## https://twitter.com/hid_tori/status/1221415683289780225
## https://twitter.com/hid_tori/status/1221415737375412224
## https://twitter.com/hid_tori/status/1221415770795728898
cat sudoku | node -e 'console.log((f=>(u=>u(u))(x=>f(y=>x(x)(y))))(f=>a=>((a,b)=>a.map(x=>x[3]).includes("*")?f(a.map(x=>x[3]!=="*"?x:[...x.slice(0,4),[...Array(9)].map((v,i)=>String(i+1)).filter(y=>!b[0][x[0]].concat(b[1][x[1]]).concat(b[2][x[2]]).includes(y))]).map(x=>x.length===4?x:x[4].length===1?[x[0],x[1],x[2],x[4][0]]:x.slice(0,4))):a)(a,a.reduce((x,y)=>(x[0][y[0]].push(y[3]),x[1][y[1]].push(y[3]),x[2][y[2]].push(y[3]),x),[...Array(3)].map(x=>[...Array(9)].map(y=>[]))).map(x=>x.map(y=>y.filter(z=>z!=="*")))))(require("fs").readFileSync("/dev/stdin","utf8").trim().split("\n").map((v,i)=>[i,v]).map(x=>[x[0],[...x[1].split("").entries()]]).flatMap(x=>x[1].map(y=>[x[0],...y])).map(x=>[x[0],x[1],Math.floor(x[0]/3)%3*3+Math.floor(x[1]/3),x[2]])).reduce((x,y)=>(x[y[0]].push(y[3]),x),[...Array(9)].map(x=>[])).map(x=>x.join("")).join("\n"))'
## Example:
# $ cat sudoku
# ***8**15*
# 9***2****
# **4*9*3**
# 6*34****7
# *4*75**2*
# **8236*91
# 2815479**
# 53*98*274
# **736*815
#
# $ cat sudoku | ...
# 362874159
# 915623748
# 874195362
# 623419587
# 149758623
# 758236491
# 281547936
# 536981274
# 497362815
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment