Advertisement
Guest User

test.ts

a guest
Apr 22nd, 2020
2,915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const ZERO = 'a'.charCodeAt(0);
  2. const WIDTH = 5;
  3.  
  4. const f = Math.floor;
  5.  
  6. function vPath(n: number): string {
  7.   if (n === 0) return '';
  8.   if (n > 0) return 'D'.repeat(n);
  9.   if (n < 0) return 'U'.repeat(-n);
  10.   return '' as never;
  11. }
  12.  
  13. function hPath(n: number): string {
  14.   if (n === 0) return '';
  15.   if (n > 0) return 'R'.repeat(n);
  16.   if (n < 0) return 'L'.repeat(-n);
  17.   return '' as never;
  18. }
  19.  
  20. function findPath([a, b]: [string, string]): string {
  21.   const x = a.charCodeAt(0) - ZERO;
  22.   const y = b.charCodeAt(0) - ZERO;
  23.   if (x === y) return '';
  24.   return vPath(f(y / WIDTH) - f(x / WIDTH)) + hPath(y % WIDTH - x % WIDTH);
  25. }
  26.  
  27. function main(input: string) {
  28.   const cs: string[] = Array.prototype.slice.call(input);
  29.   const [xs] = cs.reduce(
  30.     ([acc, last], curr) => [acc.concat([[last, curr]]), curr],
  31.     [[] as [string, string][], 'a'],
  32.   );
  33.   return xs.reduce((acc, curr) => acc + findPath(curr) + '!', '');
  34. }
  35.  
  36. console.log(main('leet'), main('code'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement