Advertisement
Guest User

Untitled

a guest
Dec 13th, 2016
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. function bunnyKill(input) {
  2. let coordinates = input[input.length - 1].split(/[,\s*]/g).map(Number)
  3. let matrix = []
  4. let killedBunnies = 0
  5. let damage = 0
  6. for (let i = 0; i < input.length - 1; i++) {
  7. matrix[i] = input[i].split(' ').map(Number)
  8. }
  9. for (let i = 0; i < coordinates.length; i+= 2) {
  10. let x = Number(coordinates[i])
  11. let y = Number(coordinates[i + 1])
  12. damage += Number(matrix[x][y])
  13. killedBunnies += 1
  14. for (let j = 0; j < matrix.length; j++) {
  15. for (let n = 0; n < matrix[j].length; n++) {
  16. if (j == x && n == y) {
  17. if (n + 1 < matrix[j].length) {
  18. matrix[j][n + 1] = matrix[j][n + 1] - matrix[x][y]
  19. }
  20. if (n - 1 >= 0) {
  21. matrix[j][n - 1] = matrix[j][n - 1] - matrix[x][y]
  22. }
  23. if (j - 1 >= 0 && n - 1 >= 0) {
  24. matrix[j - 1][n - 1] = matrix[j - 1][n - 1] - matrix[x][y]
  25. }
  26. if (j - 1 >= 0) {
  27. matrix[j - 1][n] = matrix[j - 1][n] - matrix[x][y]
  28. }
  29. if (j - 1 >=0 && n + 1 <= matrix[j].length) {
  30. matrix[j - 1][n + 1] = matrix[j - 1][n + 1] - matrix[x][y]
  31. }
  32. if (j + 1 <= matrix.length && n - 1 >= 0) {
  33. matrix[j + 1][n - 1] = matrix[j + 1][n - 1] - matrix[x][y]
  34. }
  35. if (j + 1 <= matrix.length) {
  36. matrix[j + 1][n] = matrix[j + 1][n] - matrix[x][y]
  37. }
  38. if (j + 1 <= matrix.length && n + 1 <= matrix[j].length) {
  39. matrix[j + 1][n + 1] = matrix[j + 1][n + 1] - matrix[x][y]
  40. }
  41. matrix[j][n] = 0
  42. }
  43. else
  44. continue;
  45. }
  46. }
  47. }
  48. for (let i = 0; i < matrix.length; i++) {
  49. for (let j = 0; j < matrix[i].length; j++) {
  50. if (matrix[i][j] > 0) {
  51. damage += matrix[i][j]
  52. killedBunnies += 1
  53. }
  54. }
  55. }
  56. console.log(damage)
  57. console.log(killedBunnies)
  58. }
  59. bunnyKill(['5 10 15 20',
  60. '10 10 10 10',
  61. '10 15 10 10',
  62. '10 10 10 10',
  63. '2,2 0,1'
  64. ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement