16
\$\begingroup\$

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.

\$\endgroup\$
15
  • 1
    \$\begingroup\$ Welcome 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. \$\endgroup\$
    – Rod
    Commented Jul 2, 2018 at 18:42
  • 1
    \$\begingroup\$ Related :-) \$\endgroup\$
    – Arnauld
    Commented Jul 2, 2018 at 18:42
  • 5
    \$\begingroup\$ More questions: (1) can we take a list of integers instead of an integer string? (2) can we assume the input is sorted? \$\endgroup\$ Commented Jul 2, 2018 at 18:45
  • 1
    \$\begingroup\$ I added it will be sorted and can take any input \$\endgroup\$
    – Scath
    Commented Jul 2, 2018 at 19:12
  • 2
    \$\begingroup\$ One 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. \$\endgroup\$ Commented Jul 2, 2018 at 21:26

6 Answers 6

10
\$\begingroup\$

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\$
\$\endgroup\$
5
\$\begingroup\$

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!

\$\endgroup\$
5
\$\begingroup\$

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

\$\endgroup\$
5
\$\begingroup\$

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.)
\$\endgroup\$
2
  • \$\begingroup\$ Someone said it was to soon for me to accept this do you agree? \$\endgroup\$
    – Scath
    Commented Jul 3, 2018 at 13:57
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 3, 2018 at 14:46
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.