Last active
April 25, 2020 02:32
-
-
Save lancetw/ee62e494dc1cd34ba108c2727466969c to your computer and use it in GitHub Desktop.
This file contains 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
const alphabetBoardPath = (target) => { | |
const layout = 5 | |
const hashMap = new Map(Array.from("abcdefghijklmnopqrstuvwxyz").map((c, i) => [ c, {x: (i / layout) | 0, y: i % layout | 0}])) | |
const repeat = (s, n) => (n <= 0 ? '' : s.repeat(n)) | |
let p0 = { x: 0, y: 0 } | |
return Array.from(target).reduce((ret, t) => { | |
const p = hashMap.get(t) | |
ret.push(p.y < p0.y ? repeat('L', p0.y - p.y) : '', p.x < p0.x ? repeat('U', p0.x - p.x) : '', p.x > p0.x ? repeat('D', p.x - p0.x) : '', p.y > p0.y ? repeat('R', p.y - p0.y) : '', '!') | |
p0 = p | |
return ret | |
}, []).join('') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment