Created
July 14, 2017 18:02
-
-
Save PanJ/ab7671a418cb4dddcf40cec8613ec8ce to your computer and use it in GitHub Desktop.
secretArchivesLock
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
| function secretArchivesLock(lock, actions) { | |
| const getCol = (lock, index) => { | |
| return lock.map(r => r.charAt(index)).filter((x) => x !== '.') | |
| } | |
| const getRows = (lock) => lock.map((v) => v.split("").filter((x) => x !== '.')); | |
| const getCols = (lock) => Array.apply(null, Array(lock[0].length)).map((_, i) => getCol(lock, i)) | |
| const fillLeft = (vector, length) => | |
| Array.apply(null, Array(length - vector.length)).map(() => '.').concat(vector); | |
| const fillRight = (vector, length) => | |
| vector.concat(Array.apply(null, Array(length - vector.length)).map(() => '.')); | |
| const combineRows = (vectors) => vectors.map((v) => v.join("")); | |
| const combineCols = (vectors) => vectors.reduce((prev, cur) => { | |
| cur.map((c, i) => prev[i] += c); | |
| return prev; | |
| }, Array.apply(null, Array(vectors[0].length)).map(() => "")); | |
| const acts = { | |
| L: (lock) => | |
| combineRows(getRows(lock).map((v) => fillRight(v, lock[0].length))), | |
| R: (lock) => | |
| combineRows(getRows(lock).map((v) => fillLeft(v, lock[0].length))), | |
| U: (lock) => | |
| combineCols(getCols(lock).map((v) => fillRight(v, lock.length))), | |
| D: (lock) => | |
| combineCols(getCols(lock).map((v) => fillLeft(v, lock.length))) | |
| }; | |
| return actions.split("").reduce((prev, cur) => acts[cur](prev) , lock); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment