Grok was able to figure out it was about bowling.
import java.util.List;
/**
* Scores a contest with 12 stages based on the given sequence of integer entries (0-8).
* Follows the specified rules for strikes, spares, opens, and the special handling for stage 12.
*/
public class ContestScorer {
/**
* Calculates the total contest score from the sequence of entries.
*
*
@param entries the sequence of integer entries (0 through 8)
*
@return the total score (sum of the 12 stage values, with 0 replaced by -1)
*
@throws IllegalArgumentException if there are not enough entries or invalid values
*/
public int calculateScore(List<Integer> entries) {
if (entries == null) {
throw new IllegalArgumentException("Entries list cannot be null");
}
// Convert to array for easier indexing
int[] throwsArray =
entries.stream().mapToInt(i -> i).toArray();
return calculateScore(throwsArray);
}
/**
* Calculates the total contest score from the sequence of entries.
*
*
@param entries the sequence of integer entries (0 through 8)
*
@return the total score
*/
public int calculateScore(int[] entries) {
if (entries == null) {
throw new IllegalArgumentException("Entries array cannot be null");
}
int total = 0;
int index = 0;
final int NUM_STAGES = 12;
for (int stage = 1; stage <= NUM_STAGES; stage ) {
if (index >= entries.length) {
throw new IllegalArgumentException("Not enough entries for stage " stage);
}
int first = entries[index];
validateEntry(first);
index ;
int value;
if (first == 8) {
// Strike: 8 next two (lookahead for stages 1-11)
value = 8 getBonus(entries, index, 0) getBonus(entries, index, 1);
// Only consume bonuses on the final stage
if (stage == NUM_STAGES) {
index = 2; // consume the two bonus throws
}
} else {
if (index >= entries.length) {
throw new IllegalArgumentException("Not enough entries for stage " stage);
}
int second = entries[index];
validateEntry(second);
index ;
int sum = first second;
if (sum == 8) {
// Spare: 8 next one (lookahead for stages 1-11)
value = 8 getBonus(entries, index, 0);
// Only consume bonus on the final stage
if (stage == NUM_STAGES) {
index ; // consume the bonus throw
}
} else if (sum < 8) {
// Open frame
value = sum;
} else {
throw new IllegalArgumentException("Invalid sum " sum " for stage " stage);
}
}
// Replace 0 with -1
if (value == 0) {
value = -1;
}
total = value;
}
// Optional: check that all entries were consumed (or allow extras if any)
return total;
}
private int getBonus(int[] entries, int currentIndex, int offset) {
int pos = currentIndex offset;
if (pos >= entries.length) {
throw new IllegalArgumentException("Not enough bonus entries");
}
int val = entries[pos];
validateEntry(val);
return val;
}
private void validateEntry(int entry) {
if (entry < 0 || entry > 8) {
throw new IllegalArgumentException("Entry must be between 0 and 8 inclusive");
}
}
}