Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- garden1 = [[5, 7, 8, 6, 3],
- [0, 0, 7, 0, 4],
- [4, 6, 3, 4, 9],
- [3, 1, 0, 5, 8]]
- const eatCarrots = (matrix)=>{
- //throw error when there is no input
- if (!matrix) {
- throw new Error ('please put in a matrix')
- }
- // if there is only one element, return one element
- if (matrix.length === 1 || matrix[0].length ===1 ){
- return matrix[0][0]
- }
- let results = 0
- let asleep = false;
- let center = getFirstCenter(matrix);
- while (!asleep){
- // add carrots on tracker to total
- results += matrix[center[0]][center[1]]
- // assign current spot to 0
- matrix[center[0]][center[1]] = 0
- center = getNextCenter(center[0],center[1],matrix)
- // if all the number is 0 or undefined
- //asleep = true
- if (center === undefined){
- asleep = true
- }
- }
- //return results
- return results
- }
- const getFirstCenter = (matrix) =>{
- const row = matrix.length
- const col = matrix[0].length
- let center;
- const centerRowMin = Math.floor((row-1)/2)
- const centerRowMax = Math.ceil((row-1)/2)
- const centerColMin = Math.floor((col-1)/2)
- const centerColMax = Math.ceil((col-1)/2)
- const start = {}
- start[matrix[centerRowMin][centerColMin]]= [centerRowMin,centerColMin]
- start[matrix[centerRowMin][centerColMax]] = [centerRowMin,centerColMax]
- start[matrix[centerRowMax][centerColMin]] = [centerRowMax, centerColMin]
- start[matrix[centerRowMax][centerColMax]] = [centerRowMax, centerColMax]
- const startValue = Math.max(matrix[centerRowMin][centerColMin],matrix[centerRowMin][centerColMax],matrix[centerRowMax][centerColMin], matrix[centerRowMax][centerColMax] )
- center = start[startValue]
- return center
- }
- const getNextCenter = (row, column, matrix) => {
- const validNeighboringCells = {}
- const canMoveUp = row !== 0 && matrix[row - 1][column] > 0
- const canMoveDown = row !== matrix.length - 1 && matrix[row + 1][column] > 0
- const canMoveLeft = column !== 0 && matrix[row][column - 1] > 0
- const canMoveRight = column !== matrix[0].length - 1 && matrix[row][column + 1] > 0
- let max = 0
- if (canMoveUp) {
- // let top = i - 1, j
- let upValue = matrix[row - 1][column]
- max = Math.max(max,upValue)
- validNeighboringCells[upValue]= [row - 1,column]
- }
- if (canMoveDown) {
- // let bottom = i + 1, j
- let downValue = matrix[row + 1][column]
- max = Math.max(max,downValue)
- validNeighboringCells[downValue]= [row +1,column]
- }
- if (canMoveLeft) {
- // let left = i, j-1
- let leftValue = matrix[row][column-1]
- max = Math.max(max,leftValue)
- validNeighboringCells[leftValue]= [row ,column-1]
- }
- if (canMoveRight) {
- // let right = i, j+1
- rightValue = matrix[row][column +1]
- max = Math.max(max,rightValue)
- validNeighboringCells[rightValue]= [row ,column+1]
- }
- // find the biggest number, and assign to tracker
- if (max ===0){
- return undefined
- } else {
- return validNeighboringCells[max]
- }
- }
- eatCarrots(garden1) //return 27
Add Comment
Please, Sign In to add comment