Guest User

Untitled

a guest
Jul 16th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. garden1 = [[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. const eatCarrots = (matrix)=>{
  7.  
  8. //throw error when there is no input
  9. if (!matrix) {
  10. throw new Error ('please put in a matrix')
  11. }
  12.  
  13. // if there is only one element, return one element
  14. if (matrix.length === 1 || matrix[0].length ===1 ){
  15. return matrix[0][0]
  16. }
  17.  
  18. let results = 0
  19. let asleep = false;
  20. let center = getFirstCenter(matrix);
  21.  
  22.  
  23. while (!asleep){
  24. // add carrots on tracker to total
  25. results += matrix[center[0]][center[1]]
  26. // assign current spot to 0
  27. matrix[center[0]][center[1]] = 0
  28.  
  29. center = getNextCenter(center[0],center[1],matrix)
  30.  
  31. // if all the number is 0 or undefined
  32. //asleep = true
  33. if (center === undefined){
  34. asleep = true
  35. }
  36. }
  37. //return results
  38. return results
  39. }
  40.  
  41.  
  42. const getFirstCenter = (matrix) =>{
  43.  
  44. const row = matrix.length
  45. const col = matrix[0].length
  46. let center;
  47.  
  48. const centerRowMin = Math.floor((row-1)/2)
  49. const centerRowMax = Math.ceil((row-1)/2)
  50. const centerColMin = Math.floor((col-1)/2)
  51. const centerColMax = Math.ceil((col-1)/2)
  52.  
  53. const start = {}
  54. start[matrix[centerRowMin][centerColMin]]= [centerRowMin,centerColMin]
  55. start[matrix[centerRowMin][centerColMax]] = [centerRowMin,centerColMax]
  56. start[matrix[centerRowMax][centerColMin]] = [centerRowMax, centerColMin]
  57. start[matrix[centerRowMax][centerColMax]] = [centerRowMax, centerColMax]
  58.  
  59. const startValue = Math.max(matrix[centerRowMin][centerColMin],matrix[centerRowMin][centerColMax],matrix[centerRowMax][centerColMin], matrix[centerRowMax][centerColMax] )
  60.  
  61. center = start[startValue]
  62. return center
  63. }
  64.  
  65.  
  66. const getNextCenter = (row, column, matrix) => {
  67. const validNeighboringCells = {}
  68.  
  69. const canMoveUp = row !== 0 && matrix[row - 1][column] > 0
  70. const canMoveDown = row !== matrix.length - 1 && matrix[row + 1][column] > 0
  71. const canMoveLeft = column !== 0 && matrix[row][column - 1] > 0
  72. const canMoveRight = column !== matrix[0].length - 1 && matrix[row][column + 1] > 0
  73.  
  74. let max = 0
  75.  
  76. if (canMoveUp) {
  77. // let top = i - 1, j
  78. let upValue = matrix[row - 1][column]
  79. max = Math.max(max,upValue)
  80. validNeighboringCells[upValue]= [row - 1,column]
  81. }
  82. if (canMoveDown) {
  83. // let bottom = i + 1, j
  84. let downValue = matrix[row + 1][column]
  85. max = Math.max(max,downValue)
  86. validNeighboringCells[downValue]= [row +1,column]
  87. }
  88. if (canMoveLeft) {
  89. // let left = i, j-1
  90. let leftValue = matrix[row][column-1]
  91. max = Math.max(max,leftValue)
  92. validNeighboringCells[leftValue]= [row ,column-1]
  93. }
  94. if (canMoveRight) {
  95. // let right = i, j+1
  96. rightValue = matrix[row][column +1]
  97. max = Math.max(max,rightValue)
  98. validNeighboringCells[rightValue]= [row ,column+1]
  99. }
  100. // find the biggest number, and assign to tracker
  101. if (max ===0){
  102. return undefined
  103. } else {
  104. return validNeighboringCells[max]
  105. }
  106. }
  107.  
  108. eatCarrots(garden1) //return 27
Add Comment
Please, Sign In to add comment