Score a hand of Hearts
R, 85 77 74 bytes
function(x,z=sapply(x,function(x)sum(x>0)+any(x<1)*13))abs(z-any(z>25)*26)
Unnamed function that takes an R-list as input. Works by counting the number of elements >0
and adds 13 if any element within each vector is <1
(i.e. queen of spades) and store as z
.
If any element in z
is >25
, return 26-z
, else return z
.
Try it on R-fiddle
C++14, 158 bytes
As unnamed Lambda:
[](auto c){typename decltype(c)::value_type r;int b=0;for(auto d:c){int q=0;for(auto i:d)q+=i?1:13;r.push_back(q);b+=q==26;}if(b)for(int&x:r)x=26-x;return r;}
Requires a vector<vector<int>>
and returns vector<int>
Ungolfed:
[](auto c){
typename decltype(c)::value_type r; //result vector
int b=0; //flag if one has all cards
for(auto d:c){ //over all decks
int q=0; //count points
for(auto i:d) q+=i?1:13; //+13 for queen, +1 else
r.push_back(q); //add to result
b+=q==26; //possibly activate flag
}
if(b) for(int&x:r) x=26-x; //if flag is set, mirror the results
return r;
}
Few testcases for you:
auto r = std::vector<std::vector<int>>{{2,8,7,1},{3,4},{},{9,5,6,0,10,11,12,13}};
auto s = std::vector<std::vector<int>>{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 0},{},{},{}};
auto t = std::vector<std::vector<int>>{{},{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 0},{},{1}};
Python 2, 75 72 71 bytes
i=[len(a)+12*(0in a)for a in input()]
print[[x,26-x][26in i]for x in i]
Takes input as [2, 8, 7, 1], [3, 4], [], [9, 5, 6, 0, 10, 11, 12, 13]