| Bytes | Lang | Time | Link |
|---|---|---|---|
| 104 | Scala | 231119T025041Z | 138 Aspe |
| 027 | k4 | 201024T232424Z | coltim |
| 010 | Japt h | 200524T135457Z | Shaggy |
| 048 | Wolfram Language Mathematica | 200523T173936Z | Greg Mar |
| 103 | C gcc zexecstack | 200522T094201Z | Noodle9 |
| nan | J | 200522T001403Z | Jonah |
| 066 | Io | 200522T092451Z | user9206 |
| 054 | R | 200521T230545Z | Dominic |
| 005 | 05AB1E | 200522T085208Z | user9206 |
| 016 | APL Dyalog Extended | 200522T023353Z | Bubbler |
| 057 | Python 2 | 200521T222522Z | Surculos |
| 061 | JavaScript ES6 | 200521T222859Z | Arnauld |
| 041 | Charcoal | 200521T224643Z | Neil |
| 005 | Jelly | 200521T220742Z | Unrelate |
Scala, 104 bytes
Golfed version. Attempt this online!
(a,b)=>{val A=a.sorted;var B=b.sorted.toSeq;var s=0;for(x<-A){if(B.nonEmpty&&x>B(0)){s+=1;B=B.tail;}};s}
Ungolfed version. Attempt this online!
// Function f: Calculate how many matches can be won
def f(a: Array[Int], b: Array[Int]): Int = {
val sortedA = a.sorted // Sort my team strengths
var sortedB =
b.sorted.toList // Sort opponent strengths (toList so we can drop elements easily)
var s = 0 // Number of matches that can be won
for (x <- sortedA) {
// If current member x can beat the weakest remaining opponent
if (sortedB.nonEmpty && x > sortedB.head) {
s += 1
sortedB = sortedB.tail
}
}
s
}
k4, 27 bytes
{{x+y>x}/binr/{x@<x}'(y;x)}
Another port of Jonah's J answer, this time to k4.
If the input can be taken as a single list of two vectors, e.g. (3 10 6 7 7;2 4 6 2 6) (with the opposing team as the first list item, and our team as the second), the code can be simplified to 22 bytes as:
{{x+y>x}/binr/x@'<:'x}
Wolfram Language (Mathematica), 48 bytes
Max[t=#2;Tr@Boole@Thread[#>t]&/@Permutations@#]&
Unnamed function taking two arguments as input (first our team, then their team), each as comma-separated lists such as {2,4,6,2,6}. Brute-force check over all Permutations of the input, taking the Max. Mathematica doesn't automatically compare lists elementwise, but Thread[#>t] forces it to; Boole converts Trues and Falses to 0s and 1s, respectively, so that Tr counts the number of wins. Mathematica is bad with currying when it's not programmed into the builtins, so I couldn't see a better way to handle the arguments than t=#2;.
C (gcc) -zexecstack, 124 \$\cdots\$ 105 103 bytes
Saved a whopping 2 17 19 21 bytes thanks to ceilingcat!!!
#define q(x)qsort(x,i=n,4,L"\x62b078bǃ");
i;j;f(a,b,n)int*a,*b;{q(a)q(b)for(j=0;i--;j+=*a++>b[j]);i=j;}
How
Sorts the two teams from weakest to strongest. Then goes through our side, starting from weakest, comparing it to the \$j^{\text{th}}\$ member of the opponent's team starting at \$j=0\$, their weakest wrestler. If we are ever stronger than the \$j^{\text{th}}\$ member we increment \$j\$. After we go through all our wrestlers, \$j\$ will be the maximum number we can beat.
J, 25 22 20 bytes
0(]+>)/@|.@,I.&(/:~)
-3 bytes thanks to Bubbler
Thanks to Dominic van Essen for spotting a subtle bug (now fixed)
This could be shorter using brute force, but I wanted to see how short I could make an efficient solution.
Our team = right arg, their team = left arg.
Using 2nd test case an example:
6 10 2 8 2 3 5 6 10 10 f 1 9 5 5 1 6 2 8 3 6
&(/:~)Sort both arguments:2 2 3 5 6 6 8 10 10 10 1 1 2 3 5 5 6 6 8 9I.Uses Interval Index to determine how many of their players each of our players beats:0 0 0 2 3 3 4 4 6 70...|.@,Prepend a 0 and reverse:2 3 3 4 4 6 7(]+>)/Now reduce from the right as follows: Take the right arg](the running sum, seeded at0), compare the two args using>(returns 1 if left arg is greater than running sum, 0 otherwise), and add the two.This means the running sum will increment exactly when "the number of opponents the current wrestler can beat" is greater than "opponents already beaten by other teammates".
The final result will be the total number of players our team can beat.
Io, 66 bytes
Algorithm based on Noodle9's solution.
method(x,y,F :=0;x sort map(i,F=F+if(i>y sort at(F+1),1,0))last+1)
Explanation
method(x, y, // Take 2 arguments.
F := 0 // Initialize the counter to 0.
x sort map(i, // For every item in sorted x:
F = F + if( // Add the counter by:
i>y sort at(F+1), // if the current item is larger than sorted y at the same position
1,0) // converted to an integer
) last + 1) // Add the last item of the map by 1
R, 58 54 bytes
function(a,b){for(i in sort(a))F=F+(i>sort(b)[F+1]);F}
Algorithm based on Noodle9's solution; this could be made shorter than my own original recursive solution 76 bytes or than one based on the elegant algorithm of Surculose Sputum 73 bytes in R
05AB1E, 5 bytes
Coincidental port of Unrelated String's Jelly answer.
œ€‹Oà
Explanation
œ All permutations to your team's combination
€‹ Does your team win at this position?
O Sum the resulting lists to get all wins
à Return the largest item of the list
APL (Dyalog Extended), 16 bytes
(+/⊢≥⍋)0~⍨1⊥<\⍥∧
An almost direct port of Jonah's J answer. But unfortunately, I found that, despite having the same name "interval index", J's I. and APL's ⍸ don't have the identical behavior; J finds the indices before identical elements, but APL does after them.
- J:
echo 1 3 5 I. 0 1 2 3 4 5 6gives0 0 1 1 2 2 3 - APL:
⎕←1 3 5 ⍸ 0 1 2 3 4 5 6gives0 1 1 2 2 3 3
That makes ⍸ hard to use for this problem, so I had to fall back to outer product (which is used in the previous version of Jonah's answer).
How it works
(+/⊢≥⍋)0~⍨1⊥<\⍥∧ ⍝ Left: opponent strengths, Right: ours
⍥∧ ⍝ Ascending sort both args
1⊥<\ ⍝ Outer product by < and then sum;
⍝ count the opponents who each of ours can win against
0~⍨ ⍝ Remove zeros
( ⍋) ⍝ Grade up; this is identical to ⍳∘≢ here since the arg is sorted
⊢≥ ⍝ Check if each number is at least its index
+/ ⍝ Sum; count ones
Python 2, 59 57 bytes
-2 bytes thanks to @dingledooper!
lambda a,b:sum(b.sort()<b<[x]>b.pop(0)for x in sorted(a))
A function that takes 2 lists as arguments, and returns the maximum number of matches that can be won.
Approach
For each opponent (from weakest to strongest), match that opponent with the weakest available member of my team that can win.
For example, let's say the strength of 2 teams (sorted) are as follow:
No. 1 2 3 4 5
Me [2, 2, 4, 5, 8]
Opponent [3, 6, 7, 7, 9]
- The 1st opponent has strength 3, so we need to match that with our 3rd team member.
- The 2nd opponent has strength 6, so we match that with our 5th.
We cannot win any more match. So in total we can win at most 2 matches.
Ungolfed code
a,b=map(sorted,input()) # sorted strength of my team and opponent team
s = 0 # number of matches that can be won
for x in a: # loop through my team, in order of increasing strength
if x > b[0]: # if current member can beat the weakest opponent left
s += 1 # then match those two
b.pop(0) # and remove the weakest opponent
# (otherwise, current member is useless)
print s
JavaScript (ES6), 61 bytes
A shorter version inspired by @SurculoseSputum's answer.
a=>b=>(g=a=>a.sort((a,b)=>a-b))(a).map(x=>k+=x>g(b)[k],k=0)|k
That would be 31 bytes if the arrays were already sorted in ascending order:
a=>b=>a.map(x=>k+=x>b[k],k=0)|k
JavaScript (ES6), 85 75 bytes
Takes input as two lists of integers (team, opponents).
a=>b=>a.map(x=>(b[i=b.sort((a,b)=>b-a).findIndex(y=>x>y)]=a,~i&&++k),k=0)|k
Charcoal, 41 bytes
≔I⪪η η≔I⪪ζ ζWΦη›κ⌊ζ«§≔η⌕η⌊ι⁰§≔ζ⌕ζ⌊ζχ»I№η⁰
Try it online! Link is to verbose version of code. Explanation:
≔I⪪η η
Input the ratings of your wrestlers.
≔I⪪ζ ζ
Input the ratings of your opponents.
WΦη›κ⌊ζ«
Repeat while at least one of your wrestlers can beat one of your opponents.
§≔η⌕η⌊ι⁰
Set the strength of the weakest such wrestler to 0, so it will no longer be considered.
§≔ζ⌕ζ⌊ζχ
Similarly set the strength of its opponent to 10, so it will also no longer be considered.
»I№η⁰
Count and output the number of wins.
If the input was sorted, the calculation could be done in 19 bytes. Unfortunately it takes me 30 bytes to sort the input...
Jelly, 5 bytes
Œ!>§Ṁ
The second test case times out on TIO, but I have verified it locally. Takes your lineup as the left argument and theirs as the right.
Œ! Find every permutation of your lineups' strengths,
> compare each matchup for each permutation,
§ sum your wins for each permutation,
Ṁ and return the maximum number of wins.