Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Problem2() {
- let params = arguments[0]
- let bombs = params.pop().split(' ')
- let myMatrix = MatrixReader(params)
- BombedMatrix(bombs, myMatrix)
- let totalDmg = 0
- let counter = 0
- // myMatrix.map(row => {
- // row.map(col => {
- // if (col > 0) {
- // totalDmg += col
- // counter++
- // }
- // })
- // })
- for(let i = 0; i<myMatrix.length;i++){
- for(let j =0; j<myMatrix.length;j++){
- if(myMatrix[i][j]>0){
- totalDmg+=myMatrix[i][j]
- counter++
- }
- }
- }
- console.log(totalDmg)
- console.log(counter)
- function BombedMatrix(bombBunies, matrix) {
- bombBunies.map(hopper => {
- let currentBunny = hopper.split(',').map(Number)
- let x = currentBunny[0]
- let y = currentBunny[1]
- let bunnyHp = matrix[x][y]
- //right
- if (y + 1 < matrix[0].length) {
- matrix[x][y + 1] -= bunnyHp
- }
- //left
- if (y - 1 >= 0) {
- matrix[x][y - 1] -= bunnyHp
- }
- //bot
- if (y + 1 < matrix.length) {
- matrix[x + 1][y] -= bunnyHp
- }
- //top
- if (x - 1 >= 0) {
- matrix[x - 1][y] -= bunnyHp
- }
- // right top
- if (y + 1 < matrix[0].length && x - 1 >= 0) {
- matrix[x - 1][y + 1] -= bunnyHp
- }
- // rightt bot
- if (y + 1 < matrix[0].length && x + 1 < matrix.length) {
- matrix[x + 1][y + 1] -= bunnyHp
- }
- // left bot
- if (y - 1 >= 0 && x + 1 < matrix.length) {
- matrix[x + 1][y - 1] -= bunnyHp
- }
- // left top
- if (y - 1 >= 0 && x - 1 >= 0) {
- matrix[x - 1][y - 1] -= bunnyHp
- }
- })
- return matrix
- }
- function MatrixReader(params) {
- let result = []
- for (let i = 0; i < params.length; i++) {
- result.push(params[i].split(' ').map(Number))
- }
- return result
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement