Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. var e = [[5, 7, 8, 6, 3],
  2. [0, 0, 7, 0, 4],
  3. [4, 6, 3, 4, 9],
  4. [3, 1, 0, 5, 8]];
  5.  
  6. function maxCarrots(matrix) {
  7. if (matrix.length == 1 && matrix[0].length == 1) {
  8. return matrix[0][0];
  9. } else {
  10. var center = findCenter(matrix);
  11. var visited = traverseGarden(matrix, center);
  12. return visited.reduce(function (a, b) { return a + b; }, 0);
  13. }
  14. }
  15.  
  16. function findCenter(matrix) {
  17. var height = matrix.length;
  18. var width = matrix[0].length;
  19. var midHeight = Math.floor(height / 2);
  20. var midWidth = Math.floor(width / 2);
  21. if (height % 2 == 1 && width % 2 == 1) {
  22. return [midHeight,midWidth];
  23. } else if (height % 2 == 1 && width % 2 == 0) {
  24. if (matrix[midHeight][midWidth] > matrix[midHeight][midWidth - 1]) {
  25. return [midHeight, midWidth];
  26. } else {
  27. return [midHeight, midWidth - 1];
  28. }
  29. } else if (height % 2 == 0 && width % 2 == 1) {
  30. if (matrix[midHeight][midWidth] > matrix[midHeight - 1][midWidth]) {
  31. return [midHeight, midWidth];
  32. } else {
  33. return [midHeight - 1, midWidth];
  34. }
  35. } else {
  36. var maxValues = [matrix[midHeight - 1][midWidth - 1], matrix[midHeight - 1][midWidth], matrix[midHeight][midWidth - 1], matrix[midHeight][midWidth]];
  37. var maxIndex = maxValues.reduce((maxIndex, curValue, curIndex, maxValues) => curValue > maxValues[maxIndex] ? curIndex : maxIndex, 0);
  38. switch (maxIndex) {
  39. case 0:
  40. return [midHeight - 1, midWidth - 1];
  41. case 1:
  42. return [midHeight - 1, midWidth];
  43. case 2:
  44. return [midHeight, midWidth - 1];
  45. case 3:
  46. return [midHeight, midWidth];
  47. }
  48. }
  49. }
  50.  
  51. function traverseGarden(matrix, center) {
  52. var pos = center;
  53. var centerValue = matrix[center[0]][center[1]];
  54. var visited = [centerValue]
  55. while (findMaxMove(matrix, pos)[0] > 0) {
  56. var move = findMaxMove(matrix, pos);
  57. var nextVal = move[0];
  58. var nextPos = move[1];
  59. visited.push(nextVal);
  60. pos = nextPos;
  61. }
  62. return visited;
  63. }
  64.  
  65. function findMaxMove(matrix, pos) {
  66. var x = pos[0];
  67. var y = pos[1];
  68.  
  69. var up = x-1 in matrix && matrix[x - 1][y] ? matrix[x - 1][y] : Number.NEGATIVE_INFINITY;
  70. var down = x+1 in matrix && matrix[x + 1][y] ? matrix[x + 1][y] : Number.NEGATIVE_INFINITY;
  71. var left = y-1 in matrix[0] && matrix[x][y - 1] ? matrix[x][y - 1] : Number.NEGATIVE_INFINITY;
  72. var right = y+1 in matrix[0] && matrix[x][y + 1] ? matrix[x][y + 1] : Number.NEGATIVE_INFINITY;
  73.  
  74. var moves = [up, down, left, right];
  75. var maxMove = moves.reduce((maxIndex, curValue, curIndex, moves) => curValue > moves[maxIndex] ? curIndex : maxIndex, 0);
  76.  
  77. var value = moves[maxMove];
  78.  
  79. matrix[x][y] = 0
  80.  
  81. switch (maxMove) {
  82. case 0:
  83. return [value, [x - 1, y]];
  84. case 1:
  85. return [value, [x + 1, y]];
  86. case 2:
  87. return [value, [x, y - 1]];
  88. case 3:
  89. return [value, [x, y + 1]];
  90. }
  91. }
  92.  
  93. maxCarrots(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement