TheAlgorithms-Python

Форк
0
89 строк · 3.0 Кб
1
"""
2
In the game of darts a player throws three darts at a target board which is
3
split into twenty equal sized sections numbered one to twenty.
4
5
The score of a dart is determined by the number of the region that the dart
6
lands in. A dart landing outside the red/green outer ring scores zero. The black
7
and cream regions inside this ring represent single scores. However, the red/green
8
outer ring and middle ring score double and treble scores respectively.
9

10
At the centre of the board are two concentric circles called the bull region, or
11
bulls-eye. The outer bull is worth 25 points and the inner bull is a double,
12
worth 50 points.
13

14
There are many variations of rules but in the most popular game the players will
15
begin with a score 301 or 501 and the first player to reduce their running total
16
to zero is a winner. However, it is normal to play a "doubles out" system, which
17
means that the player must land a double (including the double bulls-eye at the
18
centre of the board) on their final dart to win; any other dart that would reduce
19
their running total to one or lower means the score for that set of three darts
20
is "bust".
21

22
When a player is able to finish on their current score it is called a "checkout"
23
and the highest checkout is 170: T20 T20 D25 (two treble 20s and double bull).
24

25
There are exactly eleven distinct ways to checkout on a score of 6:
26

27
D3
28
D1  D2
29
S2  D2
30
D2  D1
31
S4  D1
32
S1  S1  D2
33
S1  T1  D1
34
S1  S3  D1
35
D1  D1  D1
36
D1  S2  D1
37
S2  S2  D1
38

39
Note that D1 D2 is considered different to D2 D1 as they finish on different
40
doubles. However, the combination S1 T1 D1 is considered the same as T1 S1 D1.
41

42
In addition we shall not include misses in considering combinations; for example,
43
D3 is the same as 0 D3 and 0 0 D3.
44

45
Incredibly there are 42336 distinct ways of checking out in total.
46

47
How many distinct ways can a player checkout with a score less than 100?
48

49
Solution:
50
    We first construct a list of the possible dart values, separated by type.
51
    We then iterate through the doubles, followed by the possible 2 following throws.
52
    If the total of these three darts is less than the given limit, we increment
53
    the counter.
54
"""
55

56
from itertools import combinations_with_replacement
57

58

59
def solution(limit: int = 100) -> int:
60
    """
61
    Count the number of distinct ways a player can checkout with a score
62
    less than limit.
63
    >>> solution(171)
64
    42336
65
    >>> solution(50)
66
    12577
67
    """
68
    singles: list[int] = [*list(range(1, 21)), 25]
69
    doubles: list[int] = [2 * x for x in range(1, 21)] + [50]
70
    triples: list[int] = [3 * x for x in range(1, 21)]
71
    all_values: list[int] = singles + doubles + triples + [0]
72

73
    num_checkouts: int = 0
74
    double: int
75
    throw1: int
76
    throw2: int
77
    checkout_total: int
78

79
    for double in doubles:
80
        for throw1, throw2 in combinations_with_replacement(all_values, 2):
81
            checkout_total = double + throw1 + throw2
82
            if checkout_total < limit:
83
                num_checkouts += 1
84

85
    return num_checkouts
86

87

88
if __name__ == "__main__":
89
    print(f"{solution() = }")
90

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.