Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const ZERO = 'a'.charCodeAt(0);
- const WIDTH = 5;
- const f = Math.floor;
- function vPath(n: number): string {
- if (n === 0) return '';
- if (n > 0) return 'D'.repeat(n);
- if (n < 0) return 'U'.repeat(-n);
- return '' as never;
- }
- function hPath(n: number): string {
- if (n === 0) return '';
- if (n > 0) return 'R'.repeat(n);
- if (n < 0) return 'L'.repeat(-n);
- return '' as never;
- }
- function findPath([a, b]: [string, string]): string {
- const x = a.charCodeAt(0) - ZERO;
- const y = b.charCodeAt(0) - ZERO;
- if (x === y) return '';
- return vPath(f(y / WIDTH) - f(x / WIDTH)) + hPath(y % WIDTH - x % WIDTH);
- }
- function main(input: string) {
- const cs: string[] = Array.prototype.slice.call(input);
- const [xs] = cs.reduce(
- ([acc, last], curr) => [acc.concat([[last, curr]]), curr],
- [[] as [string, string][], 'a'],
- );
- return xs.reduce((acc, curr) => acc + findPath(curr) + '!', '');
- }
- console.log(main('leet'), main('code'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement