Last active
May 18, 2018 13:53
-
-
Save belchior/cdbcdafd878ab14517a9bdb5701c0a7d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var countOf = value => list => list.reduce((acc, num) => num === value ? acc + 1 : acc, 0); | |
var pipe = (...fns) => data => fns.reduce((value, fn) => fn(value), data); | |
var flatten = list => list.reduce((newList, item) => [].concat(newList, item), []); | |
var padStart = length => value => list => Array(length).fill(value).concat(list).slice(list.length); | |
var transpose = list => list[0].map((_, columnIndex) => list.map(row => row[columnIndex])); | |
var groupEveryFour = list => list.reduce((newList, _, index) => { | |
return index % 4 === 0 ? [ ...newList, list.slice(index, index + 4) ] : newList; | |
}, []); | |
const removeZeros = list => list.filter(num => num !== 0); | |
var zerosToLeft = list => list.join('').replace(/0/g, '').padStart(4, '0').split('').map(Number); | |
var zerosToRight = list => list.join('').replace(/0/g, '').padEnd(4, '0').split('').map(Number); | |
var sumEquals = list => { | |
const newList = []; | |
for (let index = list.length - 1; index >= 0; index -= 1) { | |
if (list[index] === list[index - 1]) { | |
newList.push(list[index] * 2); | |
index -= 1; | |
} else { | |
newList.push(list[index]); | |
} | |
} | |
return newList.reverse(); | |
}; | |
/* | |
function sumEquals(list) { | |
if (!Array.isArray(list) || list.length <= 0) return []; | |
const tail = [ ...list ]; | |
const head = tail.shift(); | |
const nextHead = tail[0]; | |
// debugger; | |
if (!nextHead) return [head]; | |
if (head === nextHead) { | |
tail.shift(); | |
return [].concat(sumEquals(tail), head + nextHead ); | |
} else { | |
return [].concat(sumEquals(tail), head); | |
} | |
} | |
*/ | |
var printWall = list => { | |
return list.reduce((acc, item, index) => { | |
return acc + (index % 4 === 0 ? '\n' : ' ') + item; | |
}, '') + '\n'; | |
}; | |
var wall = [ | |
0,1,2,0, | |
3,0,0,4, | |
5,0,6,0, | |
0,7,0,8 | |
]; | |
var moveToTop = list => { | |
return pipe( | |
groupEveryFour, | |
transpose, | |
arr => arr.map(zerosToRight), | |
transpose, | |
flat, | |
)(list); | |
}; | |
var moveToBottom = list => { | |
return pipe( | |
groupEveryFour, | |
transpose, | |
arr => arr.map(zerosToLeft), | |
transpose, | |
flat, | |
)(list); | |
}; | |
var moveToRight = list => { | |
return pipe( | |
groupEveryFour, | |
arr => arr.map(zerosToLeft), | |
flat, | |
)(list); | |
}; | |
var moveToLeft = list => { | |
return pipe( | |
groupEveryFour, | |
arr => arr.map(zerosToRight), | |
flat, | |
)(list); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment