# Rugby scores code golf

The Rugby World Cup starts in a few hours!

Write a program or function which, given a rugby team score as input, outputs all possible ways the score could have been attained in a Rugby game.

### Ways to score in Rugby

• Penalty kick/Drop kick: Both penalty kicks and a drop kicks are worth 3 points (We regroup them in this challenge to reduce the number of possibilities).
• Try: A try is worth 5 points.
• Conversion: A conversion is worth 2 points. It can only occur if a try occurs too (i.e. there can never be more conversions than trys in a game).

### Input

Input is a positive integer, which corresponds to the total score of one team at the end of the game. You may assume that the score is valid, i.e. it can be attained using at least one combination of ways to score described above.

### Ouput

The output for one possible way of attaining the score must look like this:

2K,1T,1C


(the numbers can obviously change). the number before K is the number of penalty kicks and/or drop kicks, the number before T is the number of Trys, and the number before C is the number of conversions.

Since you have to output all possible ways the score could have been attained, you may output them either inside a list, or separate them with line breaks (which is what I'll use in the examples below).

### Test cases

• Input: 9

Output:

3K,0T,0C


There are no other possibilites (For example, 0K,1T,2C would give 9 points but a conversion cannot happen without a corresponding try).

• Input: 12

Output:

4K,0T,0C
0K,2T,1C

• Input: 19

Output:

0K,3T,2C
4K,1T,1C
3K,2T,0C

• Input: 42

Output:

14K,0T,0C
10K,2T,1C
9K,3T,0C
7K,3T,3C
6K,4T,2C
5K,5T,1C
4K,6T,0C
3K,5T,4C
2K,6T,3C
1K,7T,2C
0K,8T,1C
0K,6T,6C


### Scoring

This is , so the shortest code in bytes wins.

• Related, borderline-dupe – Peter Taylor Sep 18 '15 at 12:37
• @PeterTaylor Score is fixed, and only the total number of possibilities is required to be output in this challenge. – Fatalize Sep 18 '15 at 12:38
• I'm not sure what you mean by "score is fixed", but on reflection the constraint that tries >= conversions probably saves this from being a dupe. – Peter Taylor Sep 18 '15 at 12:45
• (nevermind for score is fixed, he explains his challenge with 78 cents and only says a few lines below that the amount can actually change). – Fatalize Sep 18 '15 at 12:49
• Tries >= conversions is a false constraint IMO. In reality you can have a Kick (3pts), a Try (5pts), or a Converted Try (7pts). Does this then make it a duplicate @PeterTaylor? – AndyT Sep 18 '15 at 16:20

# Pyth, 42 bytes

V^Q3I&g@K.[Z3jNQ1eKqQs*VKj352Ts.iKc2"K,T,C


Try it out here.

This uses the same heuristic as Andrea Biondo's answer, in that it generates every possible combination of kicks, tries and conversions from 0 to (n-1). It prints the formatted output only if (number of tries) >= (number of conversions) and the total score is correct.

                                              Implicit: Q=eval(input()), T=10, Z=0
V^Q3                                          For N in [0 - ((Q^3)-1)]:
jNQ                                Convert N to base Q to get 3-tuple
.[Z3                                   Left-pad the above with 0 to length 3
K                                       Store in K
@K       1                               K[1]
eK                             Last element of K (i.e. K[2])
g                                         Rule 1: is K[1] >= K[2]?
j352T                  [3,5,2]
*VK                       By-element multiplication of K with the above
s                          Take the sum
qQ                           Rule 2: is that sum equal to the input?
I&                                          If rules 1 & 2 are true:
c2"K,T,C        ['K,', 'T,' 'C']
.iK                Interleave K with the above
s                   Concatenate into string and output


## Python 3, 113 104 bytes

a=int(input());print(['%sK,%sT,%sC'%(i%a,i/a%a+i%a,i%a)for i in range(a**3)if i%a*3+i/a%a*5+i/a/a*7==a])


First attempt at code golf. Brute force list comprehension. I'm assuming the list output is acceptable?

Counting tries + conversion as 7 points removes the if statement. Inspired by xor's python 2 answer.

Try it here

• Yes this type of output is fine. Well done! – Fatalize Sep 18 '15 at 15:48
• Welcome to Programming Puzzles & Code Golf! – Dennis Sep 18 '15 at 16:27

# R, 135126 124 bytes

n=scan();a=expand.grid(k=0:n,t=0:n,c=0:n);with(a[a$c<=a$t&3*a$k+5*a$t+2*a$c==n,],cat(sprintf("%iK,%iT,%iC",k,t,c),sep="\n"))  Quick explanation: n=scan() # Takes input from stdin a=expand.grid(k=0:n,t=0:n,c=0:n) # Creates matrix of all possible combinations of 3 integers from 0 to n with(a[a$c<=a$t&3*a$k+5*a$t+2*a$c==n,], # Only keeps the one that produced a correct score
cat(sprintf("%iK,%iT,%iC",k,t,c),sep="\n")) # Prints


Usage:

> n=scan();a=expand.grid(k=0:n,t=0:n,c=0:n);with(a[a$c<=a$t&3*a$k+5*a$t+2*a$c==n,],cat(sprintf("%iK,%iT,%iC",k,t,c),sep="\n")) 1: 9 2: Read 1 item 3K,0T,0C > n=scan();a=expand.grid(k=0:n,t=0:n,c=0:n);with(a[a$c<=a$t&3*a$k+5*a$t+2*a$c==n,],cat(sprintf("%iK,%iT,%iC",k,t,c),sep="\n"))
1: 42
2:
14K,0T,0C
9K,3T,0C
4K,6T,0C
10K,2T,1C
5K,5T,1C
0K,8T,1C
6K,4T,2C
1K,7T,2C
7K,3T,3C
2K,6T,3C
3K,5T,4C
0K,6T,6C


# CJam, 48 bytes

Quite slow, just generates all possible combination of values from 0 to score - 1 for K, T and C. Then filters out the ones where the sum isn't right or C > T.

qi:X,3m*{_)\1=)<\[Z5Y].*:+X=&},::s"KTC"f.+',f*N*


# Explanation

qi:X                                             e# Take input as integer, assign to X
,3m*                                         e# Cartesian power [0 ... input-1]^3
{                    },                  e# Keep values if:
_)\1=)<                                 e#    Conversions <= Tries
&                    e#    AND
\[Z5Y].*:+X=                     e#    Sum([k t c]*[3 5 2]) == X
::s               e# [k t c] -> ["k" "t" "c"]
"KTC"f.+       e# Vector append -> ["kK" "tT" "cC"]
',f*   e# Join with comma
N* e# Join outer list with newline


# R, 113 101 bytes

Not quite as elegant as @plannapus's answer. Go the All Blacks :)

n=scan();for(C in 0:n)for(T in 0:n)for(P in 0:n)if(C*7+T*5+P*3==n)cat(P,'K,',C+T,'T,',C,'C\n',sep='')


Test run

> n=scan();for(C in 0:n)for(T in 0:n)for(P in 0:n)if(C*7+T*5+P*3==n)cat(P,'K,',C+T,'T,',C,'C\n',sep='')
1: 9
2:
3K,0T,0C
> n=scan();for(C in 0:n)for(T in 0:n)for(P in 0:n)if(C*7+T*5+P*3==n)cat(P,'K,',C+T,'T,',C,'C\n',sep='')
1: 12
2:
4K,0T,0C
0K,2T,1C
> n=scan();for(C in 0:n)for(T in 0:n)for(P in 0:n)if(C*7+T*5+P*3==n)cat(P,'K,',C+T,'T,',C,'C\n',sep='')
1: 42
2:
14K,0T,0C
9K,3T,0C
4K,6T,0C
10K,2T,1C
5K,5T,1C
0K,8T,1C
6K,4T,2C
1K,7T,2C
7K,3T,3C
2K,6T,3C
3K,5T,4C
0K,6T,6C

• Uh I would have never guessed that using 3 nested for loop would take less characters. Well done! – plannapus Sep 19 '15 at 6:51
• @plannapus surprised me as well – MickyT Sep 19 '15 at 6:54

## Python 2, 93

lambda n:["%dK,%dT,%dC"%(k/n/n,k%n+k/n%n,k%n)for k in range(n**3)if k%n*7+k/n%n*5+k/n/n*3==n]


An anonymous function. Generates all possible triples of values by converting a value k from 0 to n^3-1 to base n. Filters for those with the right sum. Try+conversion is considered a separate 7-point play, and the number of tries includes those too.

a#b=show a++b
f n=[k#"K,"++t#"T,"++c#"C"|q<-[[0..n]],k<-q,t<-q,c<-q,3*k+5*t+2*c==n,t>=c]


Usage example: f 19 -> ["0K,3T,2C","3K,2T,0C","4K,1T,1C"].

Edit: output in list format seems to be ok ...

# Ruby, 89

->s{(s*s).times{|i|c=s-3*(k=i%s)-5*t=i/s
c%2>0||c<0||c/2>t||puts("#{k}K,#{t}T,#{c/2}C")}}


call like this:

g=->s{(s*s).times{|i|c=s-3*(k=i%s)-5*t=i/s
c%2>0||c<0||c/2>t||puts("#{k}K,#{t}T,#{c/2}C")}}

g.call(gets.to_i)


Loops through all possible K and T and calculates the score from conversions that is required.

If the score for conversions is not invalid, print the K,T,C combination that has been found.