16
2
You are fish in a pond that needs to survive by eating other fish. You can only eat fish that are the same size or smaller than yourself. You must create a program that takes a shoal of fish as sorted input. From this you must work out how many fish you can eat and ultimately the size you will grow to.
Size chart
+--------------+--------------+--------------+--------------+
| | Amount extra | Total size 1 | Increase to |
| Current size | needed for | fish | size |
| | next size | | |
+--------------+--------------+--------------+--------------+
| 1 | 4 | 4 | 2 |
+--------------+--------------+--------------+--------------+
| 2 | 8 | 12 | 3 |
+--------------+--------------+--------------+--------------+
| 3 | 12 | 24 | 4 |
+--------------+--------------+--------------+--------------+
| 4 | 16 | 40 | 5 |
+--------------+--------------+--------------+--------------+
| 5 | 20 | 60 | 6 |
+--------------+--------------+--------------+--------------+
| 6 | 24 | 84 | 7 |
+--------------+--------------+--------------+--------------+
Rules
- Your size starts at 1
- The shoal input will contain fish integers between 0-9
- 0 = algae and wont help you feed.
- The fish integer represents the size of the fish (1-9).
- You can only eat fish the same size or less than yourself.
- You can eat the fish in any order you choose to maximize your size.
- You can only eat each fish once.
- The bigger fish you eat, the faster you grow. A size 2 fish equals two size 1 fish, size 3 fish equals three size 1 fish, and so on.
- Your size increments by one each time you reach the amounts below.
Returns an integer of the maximum size you could be
Examples
"11112222" => 3
4 fish size 1 increases to 2, 4 size 2 makes you 3
"111111111111" => 3
4 fish size 1 increases to 2, 8 size 1 makes you 3
The shortest code (counting in bytes) to do so in any language in which numbers wins.
Nice first challenge! One question though, by
You must create a function called fish
, do you mean we have to create a named function? Or would a program or an unnamed function suffice? – JungHwan Min – 2018-07-02T18:41:56.2101Welcome to PPCG, I took the liberty to do minor formatting changes in the question, feel free to rollback them if you think that they aren't appropriated. – Rod – 2018-07-02T18:42:05.923
1Related :-) – Arnauld – 2018-07-02T18:42:23.000
5More questions: (1) can we take a list of integers instead of an integer string? (2) can we assume the input is sorted? – JungHwan Min – 2018-07-02T18:45:11.660
1I added it will be sorted and can take any input – Scath – 2018-07-02T19:12:19.060
It seems like nothing prevents that from happening, but just to clarify: may the fish reach a size greater than 9? – Arnauld – 2018-07-02T19:24:30.403
1More test cases wouldn't go amiss. – Jonathan Allan – 2018-07-02T20:36:06.340
{1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9} -> 3 for example? – Mark – 2018-07-02T20:56:23.977
All of these seem to agree that 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9 is 13. I didn't check that one myself. – Mark – 2018-07-02T20:59:53.697
1...also some with algae like
0,0,0,0,0,0,0,0,1,1,1,1,1,1,2,2,2,3
->3
– Jonathan Allan – 2018-07-02T21:15:39.6702One can remove
5,6
or6,6
from Mark's last example and get to size 13; yet remove5,5
instead and one can only reach size five. – Jonathan Allan – 2018-07-02T21:26:04.520That's far too soon to be accepting a solution! – Shaggy – 2018-07-03T13:41:54.513
Oh didn't know that I figured if someone get less bytes can move it to theirs should I un-accept? – Scath – 2018-07-03T13:50:16.100
@Scath Often you don't accept any answer as comparing answers in different languages isn't particularly fair. – Sam Dean – 2018-07-03T14:33:02.770
This reminds me of the game Feeding Frenzy
– threeFatCat – 2018-07-03T16:11:55.930