Skip to content

Instantly share code, notes, and snippets.

@PanJ
Created July 14, 2017 18:02
Show Gist options
  • Select an option

  • Save PanJ/ab7671a418cb4dddcf40cec8613ec8ce to your computer and use it in GitHub Desktop.

Select an option

Save PanJ/ab7671a418cb4dddcf40cec8613ec8ce to your computer and use it in GitHub Desktop.
secretArchivesLock
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