Skip to content

Instantly share code, notes, and snippets.

@lancetw
Last active April 25, 2020 02:33
Show Gist options
  • Save lancetw/d60e1d879442bc27d04ffe4501e618dd to your computer and use it in GitHub Desktop.
Save lancetw/d60e1d879442bc27d04ffe4501e618dd to your computer and use it in GitHub Desktop.
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 }
const ret = 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('')
return ret
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment