Drill

ProblemsC++ › games

Score dice pairs

hardgamesHash mapsMathArraysC++

A dice game awards points from matching pairs. For each face value that appears more than once, every pair of matching dice contributes the face value to the total score.

dicePairsScore(dice: list<int>) → int

C++ needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

int dicePairsScore(std::vector<int> dice) {
    
}

Worked examples

CallResult
dicePairsScore(std::vector<int>{2, 3, 2, 5, 3})5
dicePairsScore(std::vector<int>{6, 6, 6})18
dicePairsScore(std::vector<int>{1, 2, 3})0
dicePairsScore(std::vector<int>{4, 4, 4, 4})24

Hint

Count frequencies, then for each face with two or more occurrences add value * count * (count - 1) / 2.

Reference solution in C++
int dicePairsScore(std::vector<int> dice) {
    std::map<int, int> freq;
    for (int d : dice) freq[d]++;
    int sum = 0;
    for (const auto& p : freq) {
        if (p.second >= 2) sum += p.first * p.second * (p.second - 1) / 2;
    }
    return sum;
}

The same problem in another language

More games problems in C++