Skip to content

Commit afe3818

Browse files
fpringlegithub-actions
authored andcommitted
Added solution for Project Euler problem 109 (TheAlgorithms#4080)
* Added solution for Project Euler problem 109 * New subscriptable builtin types * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 66096c4 commit afe3818

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

DIRECTORY.md

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
* [Binary Count Setbits](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_count_setbits.py)
3030
* [Binary Count Trailing Zeros](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_count_trailing_zeros.py)
3131
* [Binary Or Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_or_operator.py)
32+
* [Binary Shifts](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_shifts.py)
33+
* [Binary Twos Complement](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_twos_complement.py)
3234
* [Binary Xor Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_xor_operator.py)
3335
* [Count Number Of One Bits](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/count_number_of_one_bits.py)
3436
* [Reverse Bits](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/reverse_bits.py)
@@ -278,6 +280,7 @@
278280
## Graphics
279281
* [Bezier Curve](https://github.com/TheAlgorithms/Python/blob/master/graphics/bezier_curve.py)
280282
* [Koch Snowflake](https://github.com/TheAlgorithms/Python/blob/master/graphics/koch_snowflake.py)
283+
* [Mandelbrot](https://github.com/TheAlgorithms/Python/blob/master/graphics/mandelbrot.py)
281284
* [Vector3 For 2D Rendering](https://github.com/TheAlgorithms/Python/blob/master/graphics/vector3_for_2d_rendering.py)
282285

283286
## Graphs
@@ -469,6 +472,7 @@
469472
* [Runge Kutta](https://github.com/TheAlgorithms/Python/blob/master/maths/runge_kutta.py)
470473
* [Segmented Sieve](https://github.com/TheAlgorithms/Python/blob/master/maths/segmented_sieve.py)
471474
* Series
475+
* [Geometric Mean](https://github.com/TheAlgorithms/Python/blob/master/maths/series/geometric_mean.py)
472476
* [Geometric Series](https://github.com/TheAlgorithms/Python/blob/master/maths/series/geometric_series.py)
473477
* [Harmonic Series](https://github.com/TheAlgorithms/Python/blob/master/maths/series/harmonic_series.py)
474478
* [P Series](https://github.com/TheAlgorithms/Python/blob/master/maths/series/p_series.py)
@@ -757,6 +761,8 @@
757761
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_102/sol1.py)
758762
* Problem 107
759763
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_107/sol1.py)
764+
* Problem 109
765+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_109/sol1.py)
760766
* Problem 112
761767
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_112/sol1.py)
762768
* Problem 113

project_euler/problem_109/__init__.py

Whitespace-only changes.

project_euler/problem_109/sol1.py

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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] = [x for x in 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() = }")

0 commit comments

Comments
 (0)