Eating fish, growing in size

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

  1. Your size starts at 1
  2. The shoal input will contain fish integers between 0-9
  3. 0 = algae and wont help you feed.
  4. The fish integer represents the size of the fish (1-9).
  5. You can only eat fish the same size or less than yourself.
  6. You can eat the fish in any order you choose to maximize your size.
  7. You can only eat each fish once.
  8. 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.
  9. 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.

Scath

Posted 2018-07-02T18:28:41.477

Reputation: 453

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.210

1Welcome 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.670

2One can remove 5,6 or 6,6 from Mark's last example and get to size 13; yet remove 5,5 instead and one can only reach size five. – Jonathan Allan – 2018-07-02T21:26:04.520

That'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

Answers

10

JavaScript (ES6), 44 bytes

Takes input as an array of integers.

a=>a.map(x=>s+=(t+=s>=x&&x)>s*-~s*2,t=s=1)|s

Try it online!

How?

The threshold \$T_s\$ to reach the size \$s+1\$ is given by:

$$T_s=2s(s+1)$$

We keep track of our current size in \$s\$ and of what we've eaten so far in \$t\$ (we start with \$t=1\$, so this is actually off by \$1\$).

For each fish \$x\$ in the shoal, assuming it's sorted from smallest to biggest:

  • we add \$x\$ to \$t\$ (i.e. we eat the fish) whenever we have \$s\ge x\$
  • we increment \$s\$ (i.e. we grow in size) if \$t > T_s\$

Arnauld

Posted 2018-07-02T18:28:41.477

Reputation: 111 334

5

Wolfram Language (Mathematica), 40 39 bytes

(f:=Floor@s;s=1;s<#||(s+=#/4/f)&/@#;f)&

Try it online!

Explanation

f:=Floor@s;s=1;

Store floor(s) in f, symbolically. Start with s=1 (size).

... /@#

Iterate through each element in input...

s<#||(s+=#/4/f)

If the element is not greater than s, then increment s by <element> / (4 * floor(s)). The Or (||) short-circuits otherwise.

f

Return floor(s).

JungHwan Min

Posted 2018-07-02T18:28:41.477

Reputation: 13 290

5

Python 2, 60 bytes

f=lambda l,n=1,c=1:c>n*~-n*2and f(l,n+1,c+l.count(n)*n)or~-n

Try it online!

ovs

Posted 2018-07-02T18:28:41.477

Reputation: 21 408

5

Jelly, 17 bytes

J×4ÄfSR$ịx`>JTḢȯ1

Try it online!

An interesting method which could well get beaten by some kind of loop or recursion.

How?

J×4ÄfSR$ịx`>JTḢȯ1 - Link: list A (ascending digits) e.g. [1,1,1,1,1,1,1,2,2,3]
J                 - range of length                      [1,2,3,4,5,6,7,8,9,10]
 ×4               - multiply all by 4                    [4,8,12,16,20,24,28,32,36,40]
   Ä              - cumulative sums                      [4,12,24,40,60,84,112,144,180,220]
       $          - last two links as a monad (of A):
     S            -   sum                                14
      R           -   range                              [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
   f              - filter keep                          [4,12]
          `       - use left argument as right with:
         x        -   repeat elements                    [1,1,1,1,1,1,1,2,2,2,2,3,3,3]
        ị         - index into                           [      1,              3    ]
                  -                                    = [1,3]
            J     - range of length (of A)               [1,2,3,4,5,6,7,8,9,10]
           >      - greater than?                        [0,1,3,4,5,6,7,8,9,10]
                  -                1 not greater than 1---^ ^---3 is greater than 2
                  -   (note keeps values of longer - i.e. the 3,4,... here)
             T    - truthy indices                       [  2,3,4,5,6,7,8,9,10]
              Ḣ   - head                                 2
                1 - literal one                          1
               ȯ  - logical OR                           2
                  -   (edge-case handling when the head of an empty list yields 0)
                  -   (note that when the shoal is fully consumed the final size will
                  -    still be less than the length of that shoal, so TḢ will still give
                  -    this size due to >J keeping values of the longer argument.)

Jonathan Allan

Posted 2018-07-02T18:28:41.477

Reputation: 67 804

Someone said it was to soon for me to accept this do you agree? – Scath – 2018-07-03T13:57:08.683

Yes I agree; some people don't award green check marks for code-golf, others leave it about a week - accepting an answer may mean a drop in activity. As an aside I feel like this should be beatable (either in Jelly itself or as a cross-language competition) anyway! ...code-golf is a strange fit to Stack Exchange since the real competition is intra-language but the accept mark is inter-language. – Jonathan Allan – 2018-07-03T14:46:58.877

1

Haskell, 46 bytes

(!0)
a!b|sum[y|y<-a,y<=b]/2<b*b+b=b|q<-b+1=a!q

Try it online!

ovs

Posted 2018-07-02T18:28:41.477

Reputation: 21 408

1

Lua, 214 bytes

l,f=1,{}for j=1,9 do s,f[j]=(...):gsub(j,0)end::z::a,n=0,l*4 for i=1,l do a=a+i*f[i]end if a>=n then e=l while n>0 do if 0<f[e]and e<=n then n=n-e f[e]=-1+f[e]else e=e-1 end end l=l+1 else print(l)return end goto z

Try it online!

Not even near shortest one here but it was fun to figure it out :D

Lycea

Posted 2018-07-02T18:28:41.477

Reputation: 141