Guest User

Untitled

a guest
Jan 7th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. /*jshint esversion: 6*/
  2. {
  3. 'use strict';
  4.  
  5. const INPUT =
  6. `6046 6349 208 276 4643 1085 1539 4986 7006 5374 252 4751 226 6757 7495 2923
  7. 1432 1538 1761 1658 104 826 806 109 939 886 1497 280 1412 127 1651 156
  8. 244 1048 133 232 226 1072 883 1045 1130 252 1038 1022 471 70 1222 957
  9. 87 172 93 73 67 192 249 239 155 23 189 106 55 174 181 116
  10. 5871 204 6466 6437 5716 232 1513 7079 6140 268 350 6264 6420 3904 272 5565
  11. 1093 838 90 1447 1224 744 1551 59 328 1575 1544 1360 71 1583 75 370
  12. 213 166 7601 6261 247 210 4809 6201 6690 6816 7776 2522 5618 580 2236 3598
  13. 92 168 96 132 196 157 116 94 253 128 60 167 192 156 76 148
  14. 187 111 141 143 45 132 140 402 134 227 342 276 449 148 170 348
  15. 1894 1298 1531 1354 1801 974 85 93 1712 130 1705 110 314 107 449 350
  16. 1662 1529 784 1704 1187 83 422 146 147 1869 1941 110 525 1293 158 1752
  17. 162 1135 3278 1149 3546 3686 182 149 119 1755 3656 2126 244 3347 157 865
  18. 2049 6396 4111 6702 251 669 1491 245 210 4314 6265 694 5131 228 6195 6090
  19. 458 448 324 235 69 79 94 78 515 68 380 64 440 508 503 452
  20. 198 216 5700 4212 2370 143 5140 190 4934 539 5054 3707 6121 5211 549 2790
  21. 3021 3407 218 1043 449 214 1594 3244 3097 286 114 223 1214 3102 257 3345`;
  22.  
  23. const sum = (a, b) => a + b;
  24. const diffOfMaxMin = (diff, val) => {
  25. const row = val.split(/t/);
  26. const max = Math.max(...row);
  27. const min = Math.min(...row);
  28.  
  29. return diff.concat(max - min);
  30. };
  31.  
  32. const solution = INPUT.split(/n/)
  33. .reduce(diffOfMaxMin, [])
  34. .reduce(sum);
  35.  
  36. console.log("solution ", solution);
  37. }
  38.  
  39. const CumulativeDiffs = (totalSoFar, val) => {
  40. const row = val.split(/s+/);
  41. const max = Math.max(...row);
  42. const min = Math.min(...row);
  43. return totalSoFar + max - min; //add diff to cumulative total
  44. };
  45.  
  46. const solution = INPUT.split(/n/)
  47. .reduce(CumulativeDiffs, 0);
  48.  
  49. const CumulativeDiffs = (totalSoFar, val) => {
  50. let row = val.split(/s+/).map((num) => parseInt(num, 10));
  51. row.sort((a, b) => a - b); //sort numbers in each row
  52. return totalSoFar + row[(row.length - 1)] - row[0]; // add difference
  53. };
Add Comment
Please, Sign In to add comment