# Ten-Pin Bowling

In the game of ten-pin bowling, a player rolls a bowling ball down a lane to knock over pins. There are ten pins set at the end of the bowling lane. Each player has 10 frames to roll a bowling ball down a lane and knock over as many pins as possible. The first nine frames are ended after two rolls or when the player knocks down all the pins. The last frame a player will receive an extra roll every time they knock down all ten pins; up to a maximum of three total rolls. 

## The Challenge

In this challenge you will be given a string representing a player's ten frames. It will look something like this: `'X X 9/ 80 X X 90 8/ 7/ 44'` (in Java: `"X X 9/ 80 X X 90 8/ 7/ 44"`), where each frame is space-delimited, `'X'` represents strikes, and `'/'` represents spares. Your goal is take in this string of frames into a function called `bowlingScore` and return the players total score.

## Scoring

The scoring for ten-pin bowling can be difficult to understand, and if you're like most people, easily forgotten if you don't play often. Here is a quick breakdown:

### Frames

In Ten-Pin Bowling there are ten frames per game. Frames are the players turn to bowl, which can be multiple rolls. The first 9 frames you get 2 rolls maximum to try to get all 10 pins down. **On the 10th or last frame a player will receive an extra roll each time they get all ten pins down to a maximum of three total rolls. Also on the last frame bonuses are not awarded for strikes and spares moving forward.**

In this challenge, three frames might be represented like this: `54 72 44`. In this case, the player has had three frames. On their first frame they scored 9 points (5 + 4), on their second frame they scored 9 points (7 + 2) and on their third frame they scored 8 points (4 + 4). This is a very simple example of bowling scoring. It gets more complicated when we introduce strikes and spares.

### Strikes 
Represented in this challenge as `'X'`

A strike is scored when a player knocks all ten pins down in one roll. In the first 9 frames this will conclude the players turn and it will be scored as 10 points plus the points received from the next two rolls. So if a player were to have two frames `X 54`, the total score of those two frames would be 28. The first frame would be worth 19 (10 + 5 + 4) and the second frame would be worth 9 (5 + 4).

A perfect game in bowling is 12 strikes in a row and would be represented like this `'X X X X X X X X X XXX'` (in Java: `"X X X X X X X X X XXX"`). This adds up to a total score of 300.

### Spares
Represented in this challenge as `'/'`

A spare is scored when a player knocks down all ten pins in two rolls. In the first 9 frames this will be scored as 10 points plus the next roll. So if a player were to have two frames `9/ 54`, the total score of the two frames would be 24. The first frame would be worth 15 (10 + 5) and the second frame would be worth 9 (5 + 4).

For a more detailed explanation see Wikipedia: 

http://en.wikipedia.org/wiki/Ten-pin_bowling#Scoring

Source: codewars | Difficulty: MEDIUM_HARD
Tags: Ad-hoc

Starter code: def bowling_score(frames):
	

Solutions:
def bowling_score(frames):
	rolls = list(frames.replace(' ', ''))
	for (i, hit) in enumerate(rolls):
		if hit == 'X':
			rolls[i] = 10
		elif hit == '/':
			rolls[i] = 10 - rolls[i - 1]
		else:
			rolls[i] = int(hit)
	score = 0
	for i in range(10):
		frame = rolls.pop(0)
		if frame == 10:
			score += frame + rolls[0] + rolls[1]
		else:
			frame += rolls.pop(0)
			score += frame
			if frame == 10:
				score += rolls[0]
	return score


import re

def bowling_score(frames):
	frames = frames.split()
	score = 0
	for (frame, roll) in enumerate(frames, 1):
		if not roll.isdigit():
			if frame < 10:
				roll = (roll + ''.join(frames[frame:]))[:3]
		score += sum((int(r) if r.isdigit() else 10 for r in re.sub('\\d/', 'X', roll)))
	return score


def frame2rolls(frame_line: str):
	rolls = []
	for i in range(len(frame_line)):
		if frame_line[i] == 'X':
			rolls.append(10)
		elif frame_line[i] == '/':
			rolls.append(10 - rolls[-1])
		elif frame_line[i] == ' ':
			continue
		else:
			rolls.append(int(frame_line[i]))
	return rolls

def bowling_score(frame_line: str) -> int:
	score = {i: 0 for i in range(10)}
	frames = frame_line.split(' ')
	rolls = frame2rolls(frame_line)
	r = 0
	for i in range(10):
		if i == 9:
			score[i] = sum(rolls[r:])
			break
		if frames[i] == 'X':
			score[i] = 10 + rolls[r + 1] + rolls[r + 2]
			r += 1
		elif frames[i][-1] == '/':
			score[i] = 10 + rolls[r + 2]
			r += 2
		else:
			score[i] = rolls[r] + rolls[r + 1]
			r += 2
	score = [score[f] for f in score]
	return sum(score)

def generate_frames() -> str:
	frames = []
	for i in range(10):
		roll1 = random.randint(0, 10)
		if roll1 == 10:
			frames.append('X')
		else:
			roll2 = random.randint(0, 10 - roll1)
			if roll2 + roll1 == 10:
				frames.append('%d/' % roll1)
			else:
				frames.append('%d%d' % (roll1, roll2))
	if frames[-1][-1] == 'X':
		f10_r2 = random.randint(0, 10)
		if f10_r2 == 10:
			f10_r3 = random.randint(0, 10)
			if f10_r3 == 10:
				frames[-1] = 'XXX'
			else:
				frames[-1] = 'XX%d' % f10_r3
		else:
			f10_r3 = random.randint(0, 10 - f10_r2)
			if f10_r2 + f10_r3 == 10:
				frames[-1] = 'X%d/' % f10_r2
			else:
				frames[-1] = 'X%d%d' % (f10_r2, f10_r3)
	elif frames[-1][-1] == '/':
		f10_r3 = random.randint(0, 10)
		if f10_r3 != 10:
			frames[-1] = '%s%d' % (frames[-1], f10_r3)
		else:
			frames[-1] = '%sX' % frames[-1]
	return ' '.join(frames)


def bowling_score(frames):
	pins = []
	for (f, frame) in enumerate(frames.split()):
		for c in frame:
			if c == 'X':
				pins.append([10, 2, f])
			elif c == '/':
				p = 10 - pins[-1][0]
				pins.append([p, 1, f])
			else:
				pins.append([int(c), 0, f])
	for i in range(len(pins)):
		(score, offset, frame) = pins[i]
		if offset > 0 and frame < 9:
			pins[i][0] += sum((pin[0] for pin in pins[i + 1:i + 1 + offset]))
	return sum((pin[0] for pin in pins))


from collections import deque

def bowling_score(frames):
	score = 0
	next_two_rolls = deque([0, 0], 2)
	for (i, frame) in enumerate(reversed(frames.split())):
		for roll in reversed(frame):
			if roll == 'X':
				additional = sum(next_two_rolls) if i else 0
				score += 10 + additional
				next_two_rolls.appendleft(10)
			elif roll == '/':
				additional = next_two_rolls[0] if i else 0
				score += 10 + additional
				next_two_rolls.appendleft('/')
			else:
				if next_two_rolls[0] == '/':
					next_two_rolls[0] = 10 - int(roll)
				else:
					score += int(roll)
				next_two_rolls.appendleft(int(roll))
	return score


def cal(s):
	ts = 0
	for i in s:
		(c, t) = (i, 0)
		if '/' in i:
			f = i.find('/')
			c = i.replace(i[f - 1:f + 1], 'X')
		for v in c:
			if v == 'X':
				t += 10
			else:
				t += int(v)
		ts += t
	return ts

def bowling_score(frames):
	frames = frames.split()
	r = []
	for i in range(len(frames)):
		if i == 9:
			r.append(frames[i])
			continue
		elif frames[i][0] == 'X':
			r.append(''.join(frames[i:i + 3])[:3])
		elif '/' in frames[i]:
			r.append(''.join(frames[i:i + 2])[:3])
		else:
			r.append(frames[i])
	return cal(r)


def bowling_score(frames_main):
	frames = frames_main
	add = ''
	for i in range(0, len(frames_main) - 3):
		if frames_main[i] == '/':
			if frames_main[i - 1] == '0':
				frames = frames.replace('/', ' ' + 'V', 1)
				add += str(frames_main[i + 2]) + ' '
				continue
			frames = frames.replace('/', ' ' + str(10 - int(frames_main[i - 1])), 1)
			add += str(frames_main[i + 2]) + ' '
	frames = list(frames.split(' '))
	last = frames[len(frames) - 1]
	frames.remove(last)
	if last[1] == '/':
		if last[0] == '0':
			last = last.replace('/', 'V', 1)
		last = last.replace('/', str(10 - int(last[0])), 1)
	frames.append(last)
	string = ''.join(frames)
	string = string.replace('', ' ')[1:-1]
	frames = list(string.split(' '))
	while '' in frames:
		frames.remove('')
	for i in range(0, len(frames) - len(last)):
		if frames[i] == 'X':
			add += frames[i + 1] + ' ' + frames[i + 2] + ' '
	add = list(add.split(' ')[0:-1])
	string = ' '.join(frames) + ' ' + ' '.join(add)
	string = string.replace('V', '10')
	string = string.replace('X', '10')
	frames = list(string.split(' '))
	while '' in frames:
		frames.remove('')
	return sum(list(map(int, frames)))


def bowling_score(frames):
	score = frames.split(' ')
	total = 0
	for i in range(len(score) - 1):
		if calculate(score[i]) == 10:
			if 'X' in score[i]:
				if len(score[i + 1]) == 1:
					total += 10 + calculate(score[i + 1][0]) + calculate(score[i + 2][0])
				else:
					total += 10 + calculate(score[i + 1][:2])
			else:
				total += 10 + calculate(score[i + 1][0])
		else:
			total += calculate(score[i])
	if len(score[-1]) == 2:
		return total + calculate(score[-1])
	return total + calculate(score[-1][:2]) + calculate(score[-1][2:])

def calculate(s):
	if 'X' in s:
		return 10 * s.count('X')
	elif '/' in s:
		return 10
	return int(s[0]) + int(s[1]) if len(s) == 2 else int(s[0])