<commit_before><?php

class Game {
	private $rolls = array();

	public function score() {
		if ( sizeof( $this->rolls ) < 12 ) {
			throw new Exception();
		}

		$frames = $this->calculateFrames();

		if ( sizeof( $frames ) == 11 && $frames[9] < 10 ) {
			throw new Exception();
		}

		if ( sizeof( $frames ) == 11 ) {
			unset( $frames[10] );
		}

		return array_reduce( $frames, function ( $carry, $item ) {
			return $carry + $item;
		}, 0 );
	}

	public function roll( $int ) {
		if ( $int < 0 || $int > 10 ) {
			throw new Exception();
		}

		array_push( $this->rolls, $int );
	}

	/**
	 * @return array
	 * @throws Exception
	 */
	public function calculateFrames(): array {
		$frames = [];
		$next   = true;
		for ( $i = 0; $i < sizeof( $this->rolls ); $i ++ ) {
			$roll = $this->rolls[ $i ];

			if ( $next ) {
				array_push( $frames, $roll );
				if ( $frames[ sizeof( $frames ) - 1 ] == 10 ) {

					if ( sizeof( $frames ) <= 10 ) {
						if ( $i + 1 < sizeof( $this->rolls ) ) {
							$frames[ sizeof( $frames ) - 1 ] += $roll = $this->rolls[ $i + 1 ];
						} else {
							throw new Exception();
						}
						if ( $i + 2 >= sizeof( $this->rolls ) ) {
							throw new Exception();
						}
						$frames[ sizeof( $frames ) - 1 ] += $roll = $this->rolls[ $i + 2 ];
						$next = ! $next;

						if ( $this->rolls[ $i + 1 ] != 10
						     && $this->rolls[ $i + 1 ] + $this->rolls[ $i + 2 ] > 10 ) {
							throw new Exception();
						}

					}
						if ( sizeof( $frames ) == 10 ) {
							break;
						}
				}
			} else {
				if ( $frames[ sizeof( $frames ) - 1 ] < 10
				     && $frames[ sizeof( $frames ) - 1 ] + $roll > 10 ) {
					throw new Exception();
				}
				$frames[ sizeof( $frames ) - 1 ] += $roll;

				if ( $frames[ sizeof( $frames ) - 1 ] == 10 ) {
					if ( $i + 1 < sizeof( $this->rolls ) ) {
						$frames[ sizeof( $frames ) - 1 ] += $roll = $this->rolls[ $i + 1 ];
					} else {
						throw new Exception();
					}
				}
			}
			$next = ! $next;
		}

		return $frames;
	}
}<commit_msg>Problem 19: refactor<commit_after><?php

class Game {
	private $rolls = array();

	public function score() {
		if ( sizeof( $this->rolls ) < 12 ) {
			throw new Exception();
		}

		$frames = $this->calculateFrames();

		if ( sizeof( $frames ) == 11 && $frames[9] < 10 ) {
			throw new Exception();
		}

		if ( sizeof( $frames ) == 11 ) {
			unset( $frames[10] );
		}

		return array_reduce( $frames, function ( $carry, $item ) {
			return $carry + $item;
		}, 0 );
	}

	/**
	 * @return array
	 * @throws Exception
	 */
	public function calculateFrames(): array {
		$frames = [];
		$next   = true;
		for ( $i = 0; $i < sizeof( $this->rolls ); $i ++ ) {
			$roll = $this->rolls[ $i ];

			if ( $next ) {
				array_push( $frames, $roll );
				if ( $frames[ sizeof( $frames ) - 1 ] == 10 ) {

					if ( sizeof( $frames ) <= 10 ) {
						if ( $i + 1 < sizeof( $this->rolls ) ) {
							$frames[ sizeof( $frames ) - 1 ] += $roll = $this->rolls[ $i + 1 ];
						} else {
							throw new Exception();
						}
						if ( $i + 2 >= sizeof( $this->rolls ) ) {
							throw new Exception();
						}
						$frames[ sizeof( $frames ) - 1 ] += $roll = $this->rolls[ $i + 2 ];
						$next                            = ! $next;

						if ( $this->rolls[ $i + 1 ] != 10
						     && $this->rolls[ $i + 1 ] + $this->rolls[ $i + 2 ] > 10 ) {
							throw new Exception();
						}

					}
					if ( sizeof( $frames ) == 10 ) {
						break;
					}
				}
			} else {
				if ( $frames[ sizeof( $frames ) - 1 ] < 10
				     && $frames[ sizeof( $frames ) - 1 ] + $roll > 10 ) {
					throw new Exception();
				}
				$frames[ sizeof( $frames ) - 1 ] += $roll;

				if ( $frames[ sizeof( $frames ) - 1 ] == 10 ) {
					if ( $i + 1 < sizeof( $this->rolls ) ) {
						$frames[ sizeof( $frames ) - 1 ] += $roll = $this->rolls[ $i + 1 ];
					} else {
						throw new Exception();
					}
				}
			}
			$next = ! $next;
		}

		return $frames;
	}

	public function roll( $int ) {
		if ( $int < 0 || $int > 10 ) {
			throw new Exception();
		}

		array_push( $this->rolls, $int );
	}
}
{'fraction_non_alphanumeric': 0.20196319018404907, 'fraction_numerical': 0.024539877300613498, 'mean_word_length': 1.8969438521677329, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6179258', 'n_tokens_mistral': 1977, 'n_tokens_neox': 1467, 'n_words': 396}