Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Problem2vol2() {
- if (arguments[0].length == 0) {
- return
- }
- let params = arguments[0]
- let matrixLength = params[0].split(' ').length
- Bombing(MatrixReader(params, matrixLength, matrixLength), BomberReader(params, matrixLength), matrixLength)
- // Function that reads the matrix
- function MatrixReader(forRead, lines, matrixLength) {
- let result = []
- for (let i = 0; i < matrixLength; i++) {
- result[i] = Array.from(forRead[i].split(" ").map(Number))
- }
- return result
- }
- // Function that reads all cell with bombs
- function BomberReader(input, line) {
- if (input[line] == undefined) {
- return input[line]
- }
- let tempArr = input[line].split(" ")
- let bunnies = []
- tempArr.map(x => {
- let tempCor = x.split(",")
- bunnies.push([Number(tempCor[0]), Number(tempCor[1])])
- })
- return bunnies
- }
- // Function that takes the initial matrix, the cells with bomber bunies and the length of the matrix, and after that prints the total dmg and the killing spree
- function Bombing(matrixWithBunnies, bunniesWithBombs, length) {
- let totalDmg = 0
- let totalKillingSpree = 0
- if (bunniesWithBombs != undefined) {
- for (let hopper of bunniesWithBombs) {
- let y = hopper[0]
- let x = hopper[1]
- let currentHopperCell = matrixWithBunnies[y][x]
- if (currentHopperCell <= 0) {
- continue
- }
- totalDmg += currentHopperCell
- totalKillingSpree++
- // up
- if (y < length && y > 0 && x >= 0 && x < length) {
- matrixWithBunnies[y - 1][x] -= currentHopperCell
- }
- // diagonal up right
- if (y < length && y > 0 && x >= 0 && x < length - 1) {
- matrixWithBunnies[y - 1][x + 1] -= currentHopperCell
- }
- // right
- if (y >= 0 && y < length && x >= 0 && x < length - 1) {
- matrixWithBunnies[y][x + 1] -= currentHopperCell
- }
- //diagonal right bot
- if (y >= 0 && y < length - 1 && x >= 0 && x < length - 1) {
- matrixWithBunnies[y + 1][x + 1] -= currentHopperCell
- }
- // bot
- if (y >= 0 && y < length - 1 && x >= 0 && x < length) {
- matrixWithBunnies[y + 1][x] -= currentHopperCell
- }
- // diagonala bot left
- if (y >= 0 && y < length - 1 && x > 0 && x < length) {
- matrixWithBunnies[y + 1][x - 1] -= currentHopperCell
- }
- // left
- if (y >= 0 && y < length && x > 0 && x < length) {
- matrixWithBunnies[y][x - 1] -= currentHopperCell
- }
- // diagonal left top
- if (y > 0 && y < length && x > 0 && x < length) {
- matrixWithBunnies[y - 1][x - 1] -= currentHopperCell
- }
- matrixWithBunnies[y][x] = 0
- }
- }
- for (let i = 0; i < length; i++) {
- for (let j = 0; j < length; j++) {
- if (matrixWithBunnies[i][j] > 0) {
- totalKillingSpree++
- totalDmg += matrixWithBunnies[i][j]
- }
- }
- }
- console.log(totalDmg)
- console.log(totalKillingSpree)
- }
- }
- Problem2vol2([])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement