Advertisement
imamatory

raif challenge digital calc

Dec 6th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const a = [];
  2. const b = [];
  3.  
  4. for (let i = 10; i < 100; i++) {
  5.   a.push(i);
  6.   b.push(i);
  7. }
  8.  
  9. const digitCodesSrc = {
  10.   '0': '1111110',
  11.   '1': '0110000',
  12.   '2': '1101101',
  13.   '3': '1111001',
  14.   '4': '0110011',
  15.   '5': '1011011',
  16.   '6': '1011111',
  17.   '7': '1110000',
  18.   '8': '1111111',
  19.   '9': '1111011',
  20. };
  21.  
  22. const digitCodes = Object.fromEntries(
  23.   Object.entries(digitCodesSrc).map(([key, val]) => [
  24.     key, val.split('').map((x) => Number(x)),
  25.   ]),
  26. );
  27.  
  28. const result = [];
  29.  
  30. for (const a1 in a) {
  31.   for (const b1 in b) {
  32.     if (a1 * b1 >= 100 && a1 * b1 < 10000) {
  33.       result.push([a1, b1, a1 * b1]);
  34.     }
  35.   }
  36. }
  37.  
  38. const zip = (arr1, arr2) => arr1.map((x, i) => [x, arr2[i]]);
  39. const padLeft = (s, ord) => {
  40.   const padCount = ord - s.length;
  41.   const padStr = new Array(padCount).fill(0);
  42.   return `${padStr}${s}`;
  43. }
  44.  
  45. const countDigitDistance = (x, y) => (
  46.   zip(digitCodes[x], digitCodes[y])
  47.     .reduce((acc, [x1, y1]) => {
  48.       const diff = y1 - x1;
  49.       if (diff === -1) return [acc[0] + 1, acc[1]];
  50.       if (diff === 1) return [acc[0], acc[1] + 1];
  51.       return acc;
  52.     }, [0, 0])
  53. );
  54.  
  55. const countDistance = (s1, s2) => (
  56.   zip(s1.split(''), s2.split(''))
  57.     .reduce(
  58.       (acc, x) => {
  59.         const d = countDigitDistance(x[0], x[1]);
  60.         const res = [acc[0] + d[0], acc[1] + d[1]];
  61.         return res;
  62.       },
  63.       [0, 0],
  64.     )
  65. );
  66.  
  67. const srcNum = '25691725';
  68.  
  69. const final = result.map(([a1, b1, c1]) => [a1, b1, c1.toString()])
  70.   .map(([a1, b1, c1]) => [padLeft(a1, 2), padLeft(b1, 2), padLeft(c1, 4)])
  71.   .filter(([a1, b1, r]) => {
  72.     const t = `${a1}${b1}${r}`;
  73.     const diff = countDistance(srcNum, t);
  74.     return diff[0] === 5 && diff[1] === 5;
  75.   });
  76.  
  77. console.log(final);
  78. console.log(final.length);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement