Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve (arr, num) {
- let [damagePerHit, stopNumber, rounds] = [
- Math.min(...arr),
- Math.max(...arr),
- 1
- ]
- let firstPart = arr.slice(0, arr.length / 2)
- let secondPart = arr.slice(arr.length / 2)
- let firstChunks = []
- let secondChunks = []
- while (firstPart.length > 0) {
- firstChunks.push(firstPart.splice(0, num))
- secondChunks.push(secondPart.splice(0, num))
- }
- let firstGiant = firstChunks
- .map(x => x.reduce((acc, el) => acc * el))
- .reduce((acc, el) => acc + el, 0)
- let secondGiant = secondChunks
- .map(x => x.reduce((acc, el) => acc * el))
- .reduce((acc, el) => acc + el, 0)
- if (damagePerHit !== 0) {
- while (firstGiant > stopNumber && secondGiant > stopNumber) {
- firstGiant -= damagePerHit
- secondGiant -= damagePerHit
- rounds++
- }
- }
- console.log(
- firstGiant === secondGiant
- ? `Its a draw ${firstGiant} - ${secondGiant}`
- : firstGiant > secondGiant
- ? `First Giant defeated Second Giant with result ${firstGiant} - ${secondGiant} in ${rounds} rounds`
- : `Second Giant defeated First Giant with result ${secondGiant} - ${firstGiant} in ${rounds} rounds`
- )
- }
Advertisement
Add Comment
Please, Sign In to add comment