Setup
Unfortunately, you have found yourself in a difficult situation. You have injured your leg and are unable to walk. A number n
of zombies are shuffling towards you, intent on eating your brains. Luckily, you've got your trusty rifle.
The zombies will start at a range of r
meters. They will move at 0.5m per second. Each second, you shoot one zombie, which depletes your ammo reserve a
by 1 each shot. The remaining zombies shamble forward. Since you're a good shot but also debilitated, there's a 5% chance you might miss.
If any of the zombies manage to reach 0 meters, you get eaten and lose. If you run out of ammo, you'll also get eaten. Ignore any time that would be spent reloading.
Write a function that accepts the total number of zombies n
, a range in meters r
, and the number of bullets you have a
.
If you shoot all the zombies, return "You shot all X zombies." If you get eaten before killing all the zombies, and before running out of ammo, return "You shot X zombies before being eaten: overwhelmed." If you run out of ammo before shooting all the zombies, return "You shot X zombies before being eaten: ran out of ammo." (If you run out of ammo at the same time as the remaining zombies reach you, return "You shot X zombies before being eaten: overwhelmed.".)
Example
zombie_shootout(3, 10, 10)
=> "You shot all 3 zombies."
Tests
zombie_shootout(100, 8, 200)
zombie_shootout(50, 10, 8)
Good luck! (I think you're going to need it.)
This challenge comes from Captain_Howdy 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 (10)
Something like this should do the trick in Elixir. I'm just doubling the range and subtracting one on each iteration to avoid issues with floats and because working with integers is faster.
The 5% is throwing me off. There's no way to compute a certain live/die verdict when a probability. A singular zombie could be 2000 away and you get really unlucky and miss all shots.
Maybe you mean every 20th shot you miss? Or create an impure function that returns a different value depending on random chance from the probability? Or something else?
I believe it would mean something like that in Haskell (I'm not really confortable enough with Monad so there is room to improvements).
And I would use this on each shot. Meaning I have 5/100 chance of missing my target. That is what I understood but I may be wrong on this one.
This was the problem that I couldn't work out either - the tests become flaky and unreliable. Seems at odds with the rest of the scenario
Python, where of course the expected number of zombies killed is a maximum and sometimes the function says less were killed.
In Python
here's my solution in Python
C++
Python