r/dailyprogrammer • u/nint22 1 2 • Jan 23 '13
[01/23/13] Challenge #118 [Intermediate] Canon Timing
(Intermediate): Canon Timing
Naval ships typically separate their shells, explosives, and cannons in different compartments. This is all done for the safety of the ship and control of the explosive materials. Check out this great animation from Wikipedia on how some ships load cannons!
Your job, as a first-class naval programmer, is to simulate how many shells can be fired given how long you want the system to work and how many seconds each sub-system does "work". Assume your system only has three components (shells, propellant, and the cannon), with each component having a different "work" times and with the cannon having a dependency on the shells and propellant loading.
The simulation should implement the following behaviors:
Shell and propellant do work independently, that is to say they are not dependent on one another.
Shell and propellant can only start re-working once they pass their materials to the cannon if, and only if, the canon is not firing.
The cannon can only fire if both shell and propellant are given to it. This is to say there is no "queue" where the cannon is that can keep a small stock of shells and propellant; it must only have one of each while firing.
Upon simulation initialization, all sub-systems must start their timers from 0. (i.e. the simulation starts in a default position of no work having been done)
Note that when firing the cannon, you can count a "shot fired" immediately, but can only reload once the work-time has been consumed.
As an example, let shell and propellant retrieval take two seconds each. Let gun firing take one second. Your simulation will first take two seconds to get both the shell and propellant to the cannon. The cannon can then fire, consuming one second of work. During this one second of work, your shell and propellant retrievals can start, such that it only takes one more second for the cannon to wait before being able to fire again. Thus if you only simulated for three seconds, your cannon would only fire once, but if you simulated for five seconds, it would fire twice, if simulated for seven seconds, it would fire thrice, etc.
Author: nint22
Formal Inputs & Outputs
Input Description
Expect four decimal numbers (up to two decimal points in precision, so a format like "<some integers or zero>.<two integer decimals, or double-zero>") on standard input (console) delimited by a space character. Let these four decimals, in order, be N A B and C. N is the time you want the firing system to be simulated. A and B are, respectively, the work times for shell and propellant retrieval. Finally, C is the time it takes to fire the cannon.
Output Description
Simply print, as an integer, how many times the cannon can successfully fire in the given simulation time. Please note that a cannon's shot can be counted before the cannon's work time is past.
Sample Inputs & Outputs
Sample Input
5.00 2.00 2.00 1.00
Sample Output
2
Challenge Input
99.99 1.23 3.21 5.01
Challenge Input Solution
Not yet posted (Check back in a few days)
Note
This challenge is not as trivial as it appears, since simulating non-integer times will require internal abstraction of the time mechanism. Casting the input to floats or doubles will lead to errors when given large simulation times.
3
u/robin-gvx 0 2 Jan 23 '13
This is a bit pedantic, but:
s/canon/cannon/Compare and contrast canon vs cannon.
8
u/Ttl Jan 23 '13 edited Jan 23 '13
Python one-liner. Seems to work correctly but I'm not entirely sure:
EDIT: Explanation:
3
u/skeeto -9 8 Jan 24 '13
Lisp. Unless I'm misunderstanding something, this has a trivial closed-form solution.
If parsing the input is important:
Challenge output:
2
u/adzeitor 0 0 Jan 23 '13
haskell with Fixed nums (use -v argument for verbose)
challenge output (warning, maybe spoiler )
source:
2
u/jtimtaylor 0 0 Jan 23 '13
Python. Less mathematical, more of a simulation.
Solution:
Edit: Formatting
2
u/srhb 0 1 Jan 23 '13 edited Jan 23 '13
The wording confuses me. Bullet two seems to me to imply that shell and propellant counters may NOT begin until after firing has finished, even if the shot counts immediately after the loading process.
With that interpretation, and with fixed precision arithmetic, Haskell (in a slightly contrived form):
Challenge output:
Edit: For completeness, here's the solution with the other interpretation, the one given in the text part of the problem:
Challenge output:
2
u/pdewacht 0 1 Jan 23 '13 edited Jan 23 '13
Scheme (R5RS) solution, based on recursive lazy lists. This being Scheme, we sadly have to set up our own infrastructure, but when that's done the solution is really neat. Tested on Guile 2.0 and Gauche. (and yeah, I depend on the Scheme implementation having rational numbers.)
2
u/robotreader Jan 24 '13
C. I may have severely misunderstood the problem, but it seems like it's bound by the slowest operation. The other two will always be waiting for that one to finish, so find how often the slowest operation can run, and that's your answer.
I also multiplied everything by 100 through removing the decimal so I wouldn't have to deal with floating-point error. String manipulation is fun!
2
u/the_mighty_skeetadon Jan 29 '13 edited Jan 29 '13
Simple Ruby:
3
u/Sonnenhut Jan 23 '13 edited Jan 23 '13
Java:
A little blown up, but easier to understand than some (shorter) other code
EDIT: TIL there is:
2
u/blexim Jan 23 '13 edited Jan 23 '13
1
u/bheinks 0 0 Jan 23 '13 edited Jan 25 '13
Python
Edit: simplified
1
u/lawlrng 0 1 Jan 23 '13 edited Jan 24 '13
Well, that was fun! I took the note to heart and wrote a simple class for dealing with the time. =) Done in Python.
Edit: Updated to take into account that a shot is counted before its work is past.
Challenge output:
1
u/minno Jan 23 '13
Python solution:
Challenge input solution:
1
u/rftz Jan 24 '13 edited Jan 24 '13
I may be missing something, but wouldn't this work (Java):
We can avoid Double trouble by simply ignoring the decimal point and treat them as ints, 100 times larger.
1
u/kingnez Jan 24 '13
C. Challenge Output: 20.
1
u/jeff303 0 2 Jan 24 '13 edited Jan 24 '13
Closed form solution, in Python.
Input (including challenge) 1.00 2.00 2.00 1.00 2.00 2.00 2.00 1.00 3.00 2.00 2.00 1.00 4.00 2.00 2.00 1.00 5.00 2.00 2.00 1.00 6.00 2.00 2.00 1.00 7.00 2.00 2.00 1.00 8.00 2.00 2.00 1.00 9.00 2.00 2.00 1.00 99.99 1.23 3.21 5.01
Output (including challenge)
1
u/[deleted] Jan 24 '13
In the first try I had the interpretation of srhb (that shell and propellant loaders cannot work while cannon Firing Time is running) which changes the output to 1 for sample, and 12 for challenge.
I changed the implementation to be in conformance with the text-interpretation (c++11):
Note: I use a simple header to simplify typing and reduce verbosity.
1
u/covega Jan 24 '13
Beginners attempt at a very crude (but easy to follow) Java solution advice would be appreciated!
1
u/[deleted] Jan 25 '13
Here is it in Go, based off Ttl's solution:
Output for challenge:
1
u/Schmenge470 Apr 09 '13
Java: http://pastebin.com/EGn92gYM
Output:
0
u/[deleted] Jan 24 '13
[deleted] — view deleted comment