Advertisement
xapu

Untitled

Jun 3rd, 2017
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. function Problem2() {
  2. let params = arguments[0]
  3. let bombs = params.pop().split(' ')
  4. let myMatrix = MatrixReader(params)
  5. BombedMatrix(bombs, myMatrix)
  6.  
  7. let totalDmg = 0
  8. let counter = 0
  9.  
  10. // myMatrix.map(row => {
  11. // row.map(col => {
  12. // if (col > 0) {
  13. // totalDmg += col
  14. // counter++
  15. // }
  16. // })
  17. // })
  18.  
  19. for(let i = 0; i<myMatrix.length;i++){
  20. for(let j =0; j<myMatrix.length;j++){
  21. if(myMatrix[i][j]>0){
  22. totalDmg+=myMatrix[i][j]
  23. counter++
  24. }
  25. }
  26. }
  27.  
  28. console.log(totalDmg)
  29. console.log(counter)
  30.  
  31. function BombedMatrix(bombBunies, matrix) {
  32. bombBunies.map(hopper => {
  33. let currentBunny = hopper.split(',').map(Number)
  34. let x = currentBunny[0]
  35. let y = currentBunny[1]
  36. let bunnyHp = matrix[x][y]
  37. //right
  38. if (y + 1 < matrix[0].length) {
  39. matrix[x][y + 1] -= bunnyHp
  40. }
  41. //left
  42. if (y - 1 >= 0) {
  43. matrix[x][y - 1] -= bunnyHp
  44. }
  45. //bot
  46. if (y + 1 < matrix.length) {
  47. matrix[x + 1][y] -= bunnyHp
  48. }
  49. //top
  50. if (x - 1 >= 0) {
  51. matrix[x - 1][y] -= bunnyHp
  52. }
  53. // right top
  54. if (y + 1 < matrix[0].length && x - 1 >= 0) {
  55. matrix[x - 1][y + 1] -= bunnyHp
  56. }
  57. // rightt bot
  58. if (y + 1 < matrix[0].length && x + 1 < matrix.length) {
  59. matrix[x + 1][y + 1] -= bunnyHp
  60. }
  61. // left bot
  62. if (y - 1 >= 0 && x + 1 < matrix.length) {
  63. matrix[x + 1][y - 1] -= bunnyHp
  64. }
  65. // left top
  66. if (y - 1 >= 0 && x - 1 >= 0) {
  67. matrix[x - 1][y - 1] -= bunnyHp
  68. }
  69.  
  70. })
  71. return matrix
  72. }
  73. function MatrixReader(params) {
  74. let result = []
  75. for (let i = 0; i < params.length; i++) {
  76. result.push(params[i].split(' ').map(Number))
  77. }
  78. return result
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement