Skip to content

Instantly share code, notes, and snippets.

@QuadFlask
Created March 29, 2016 13:46
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 QuadFlask/ffe09bd86811bddc22af to your computer and use it in GitHub Desktop.
Save QuadFlask/ffe09bd86811bddc22af to your computer and use it in GitHub Desktop.
[CodeWars] 스도쿠 벨리데이터

http://www.codewars.com/kata/529bf0e9bdf7657179000008/train/javascript;

처음에는 합이 45가 나오니까 합을 모두 계산해서 해볼까...

했다가 그럴필요 없이 각 로우/컬럼/3x3박스에 1~9가 모두 들어있으면 되겠다 해서 이렇게 했는데;;

그냥 숫자가 모두 있다를 체크하는게 나을거 같아서 그냥 해봄;;

var hasAll = (arr) => arr.reduce((a,b)=>a+b)==45

그런데, 사실 subtract 메서드로 그냥 해버릴 수 있었는데 지원을 안해서;;;젠장

var hasAll = (arr) => arr.subtract(b).length==0; // not work on CodeWars(Node v0.10.21)
var hasAll = (arr) => n.every(a=>arr.indexOf(a)>=0);
var n = [1,2,3,4,5,6,7,8,9];
var box = (arr,x,y) => arr.slice(y*3,y*3+3).map(a=>a.slice(x*3,x*3+3)).reduce((a,b)=>a.concat(b));
var hasAll = (arr) => n.every(a=>arr.indexOf(a)>=0);

return [board.map(hasAll),
	n.map((_,i)=>board.map(a=>a[i])).map(hasAll),
	n.map((_,i)=>box(board, ~~(i/3), i%3)).map(hasAll)].reduce((a,b)=>a.concat(b)).every(a=>a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment