Guest User

Untitled

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