DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #129 - Clay Pigeon Shooting

Pete and his mate Phil are out in the countryside shooting clay pigeons with a shotgun - amazing fun. Some of the targets have something attached to create lots of smoke when hit, guaranteed by the packaging to generate 'real excitement!'

They decide to have a competition. 3 rounds, 2 shots each. Winner is the one with the most hits.

For each round you will receive the following format:

[{P1:'XX', P2:'XO'}, true]

That is an array containing an object and a boolean. Pl represents Pete, P2 represents Phil. X represents a hit and O represents a miss. If the boolean is true, any hit is worth 2. If it is false, any hit is worth 1.

Find out who won. If it's Pete, return 'Pete Wins!'. If it is Phil, return 'Phil Wins!'. If the scores are equal, return 'Draw!'.

Note that as there are three rounds, the actual input (x) will look something like this:

[[{P1:'XX', P2:'XO'}, true], [{P1:'OX', P2:'OO'}, false], [{P1:'XX', P2:'OX'}, true]]

Test Cases:

shoot([[{P1:'XX', P2:'XO'}, false], [{P1:'OX', P2:'XX'}, false], [{P1:'OO', P2:'XX'}, true]])

shoot([[{P1:'XX', P2:'XO'}, true], [{P1:'OX', P2:'OO'}, false], [{P1:'XX', P2:'OX'}, true]])

Good luck!


This challenge comes from A.Partridge on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (4)

Collapse
 
hiddewie profile image
Hidde Wieringa

A try using JavaScript:

const playerScore = (player) => player.split('').filter(it => it === 'O').length
const calculateScore = (data) => data.reduce((score, [{P1, P2}, double]) => score + (double ? 2 : 1) * playerScore(P1) - playerScore(P2), 0)
const printScore = s => (s === 0 ? "Draw!" : (s > 0 ? "Pete Wins!" : "Phil Wins!"))
const shoot = s => printScore(calculateScore(s))
Collapse
 
chandrakumarreddy profile image
Chandra

working code for clay pigeon shooting

Collapse
 
hiddewie profile image
Hidde Wieringa

Oh it seems I took a hit for a miss and the other way around πŸ˜‰

Collapse
 
erezwanderman profile image
erezwanderman

JS one line

shoot = tc => tc.map(round => [...round[0].P1].reduce((a, b, c, d) => a + ((b === 'X')) * (round[1] + 1), 0)).reduce((a, c) => a + c, 0) > tc.map(round => [...round[0].P2].reduce((a, b, c, d) => a + ((b === 'X')) * (round[1] + 1), 0)).reduce((a, c) => a + c, 0) ? 'Pete Wins!' : (tc.map(round => [...round[0].P1].reduce((a, b, c, d) => a + ((b === 'X')) * (round[1] + 1), 0)).reduce((a, c) => a + c, 0) === tc.map(round => [...round[0].P2].reduce((a, b, c, d) => a + ((b === 'X')) * (round[1] + 1), 0)).reduce((a, c) => a + c, 0) ? 'Draw!' : ('Phil Wins!'))