1
\$\begingroup\$

Given a positive input \$n > 0\$, output the amout of two types based on their rarity. The two types are called \$A\$ and \$B\$, we know the followings:

  • \$n\$ is a limited input and the maximum is \$nmax\$
  • At the start \$B\$ is twice as rare as \$A\$
  • As the presence of \$A\$ increade the rarity of \$B\$ decreases
  • When \$n\$ = \$nmax\$, \$A\$ will be equal to \$B\$
  • When \$n\$ is not \$nmax\$, \$A\$ cannot be equal to \$B\$
  • The value of \$A\$ and \$B\$ cannot be decimal.

Example

nmax = 10

n = 1, output = [1A, 0B]
n = 2, output = [2A, 0B]
n = 3, output = [2A, 1B]
n = 4, output = [3A, 1B]
n = 5, output = [3A, 2B]
n = 6, output = [4A, 2B]
n = 7, output = [4A, 3B]
n = 8, output = [5A, 3B]
n = 9, output = [5A, 4B]
n = 10, output = [5A, 5B]

Shortest code win.

\$\endgroup\$
8
  • 1
    \$\begingroup\$ So we have \$n = A+B\$ for any \$n\$ and \$nmax = 2A = 2B\$. Is that correct? If so, is \$nmax\$ guaranteed to be even? \$\endgroup\$ – Arnauld Apr 12 at 9:37
  • 2
    \$\begingroup\$ Is \$nmax\$ another output? Or is it a constant which will always be \$10\$? May I output some values different to current one while it meets all requirements here? Or should I output exactly the same values as the example shown? \$\endgroup\$ – tsh Apr 12 at 9:51
  • \$\begingroup\$ Say may I implement \$A_{nmax}(n)=\min\left\{n, nmax\right\}\$, \$B_{nmax}(n)=\max\left\{n - nmax, 0\right\}\$? \$\endgroup\$ – tsh Apr 12 at 9:53
  • 3
    \$\begingroup\$ Sorry it's my first question in this stack, i'll try to edit and be more clear. \$\endgroup\$ – G. Ciardini Apr 12 at 10:20
  • 2
    \$\begingroup\$ Welcome to Code Golf and nice first question! For future reference, we recommend using the Sandbox to get feedback on challenge ideas before posting them to main \$\endgroup\$ – caird coinheringaahing Apr 12 at 12:25
6
\$\begingroup\$

JavaScript (Node.js), 25 bytes

m=>n=>[a=n/2+(n<m)|0,n-a]

Try it online!

JavaScript (Node.js), 24 bytes

m=>n=>[b=n-(n<m)>>1,n-b]

Try it online!

\$\endgroup\$
4
\$\begingroup\$

JavaScript (ES6), 28 bytes

Expects (nmax)(n). Returns [B,A].

(This is based on my current understanding of the task.)

m=>n=>[b=n/2-(n<m&~n)|0,n-b]

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Python, 266 63 bytes

And we're down to less than half one-third one-quarter two-sevenths one-quarter of my original, so a lot less scuffed than anything I've put out, but it's a first answer:

def a(b,c):
 m=(b<c)*(1-.5*(b%2))
 return int(b/2+m),int(b/2-m)

Takes advantage of the fact that \$\frac {a+1} {b+1} < \frac a b\$, if \$a > b\$: after that, it's just handling evens and a few exception cases.

-58 due to Wasif and a lambda of str(int(n)), plus dropping of whitespace.

-24 due to Wasif and the lambda function 2.

-14 by Wasif again and more cleaner conditionals.

-5 due to Wasif again by dropping the last else.

-15/-1 to Dominic van Essen and Lyxal respectively.

-3 from Wasif (making a return) and -6 from Dominic Van Essen again.

-39 from pxeger due to input semantics, taking us under halfway.

-9 from pxeger and -7 from Lyxal, taking us under 100, by returning a tuple instead of a list.

-2 from Dominic Van Essen, also managing to fix an error in the process

-14 from Dominic Van Essen again, with a nice simplification.

So far, total bytes saved:

  • Wasif: 104
  • pxeger: 48
  • Dominic Van Essen: 47
  • Lyxal: 8
\$\endgroup\$
1
1
\$\begingroup\$

Husk, 11 10 bytes

Se≠¹÷2+¹←=

Try it online!

Se≠¹÷2+¹←=      # full program:
Se≠⁰÷2+⁰←=²⁰    # here with implicit final arguments added for clarity;
      +⁰        # add to n:
        ←       # one less than
         =²⁰    # 1 if n==m
                # (in other words, subtract 1 from n if n==m),
    ÷2          # now integer divide by 2,
Se              # and construct a 2-element list of the result together with
  ≠⁰            # the difference between itself and n
\$\endgroup\$

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