Skip to content

Instantly share code, notes, and snippets.

@svidovich
Last active July 20, 2021 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svidovich/c46bf61eae4d919122ba2e8eb2b06358 to your computer and use it in GitHub Desktop.
Save svidovich/c46bf61eae4d919122ba2e8eb2b06358 to your computer and use it in GitHub Desktop.
Code for "Extraordinarily Bland Analysis of a Game of Darts"!
from typing import List
dartboard = [
20,
1,
18,
4,
13,
6,
10,
15,
2,
17,
3,
19,
7,
16,
8,
11,
14,
9,
12,
5
]
segment_size = 6
def get_region_scores(dartboard: List[int]) -> dict:
region_scores = dict()
cursor = 0
dartboard_size = len(dartboard)
while True:
# Get the current segment via the cursor. Use modular arithmetic to detect whether we're
# looping back on ourselves or not. If we are, then get the appropriate elements of the
# dartboard by taking the remainder of the list and concatenating it with the correct
# count, wrapped around.
segment_end = (cursor + (segment_size - 1)) % dartboard_size
current_segment = list()
if cursor > segment_end:
current_segment: list = dartboard[cursor:] + dartboard[:segment_end + 1]
else:
current_segment: list = dartboard[cursor:segment_end + 1]
# Build a key for this segment. If we've seen it before, bust out.
segment_key: str = ','.join([str(x) for x in current_segment])
if segment_key in region_scores:
break
# Get some averages together for the parts of the current segment
segment_closed_interval = sum(current_segment) / len(current_segment)
segment_left_open = sum(current_segment[1:]) / len(current_segment[1:])
segment_right_open = sum(current_segment[:-1]) / len(current_segment[:-1])
segment_open_interval = sum(current_segment[1:-1]) / len(current_segment[1:-1])
segment_average = (segment_closed_interval + segment_left_open + segment_right_open + segment_open_interval) / 4
region_scores[segment_key] = {
'segment_average': segment_average,
'closed_interval': segment_closed_interval,
'left_open': segment_left_open,
'right_open': segment_right_open,
'open_interval': segment_open_interval
}
# Make sure the cursor doesn't go too far!
cursor = (cursor + 1) % dartboard_size
return region_scores
def get_best_scores(scores):
metrics = [
'segment_average',
'closed_interval',
'left_open',
'right_open',
'open_interval',
]
for metric in metrics:
best_score_for_metric = list(sorted(scores.items(), key=lambda x: x[1][metric], reverse=True))[0]
region = best_score_for_metric[0]
region_data = best_score_for_metric[1][metric]
print(f'The best region to hit for the metric {metric} is {region}, carrying an average of {region_data}')
def main():
scores: dict = get_region_scores(dartboard)
get_best_scores(scores)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment