Skip to content

Instantly share code, notes, and snippets.

@iporsut
Created November 11, 2017 03:00
Show Gist options
  • Save iporsut/894aeaf0952341ae8c2a98a5da5a2383 to your computer and use it in GitHub Desktop.
Save iporsut/894aeaf0952341ae8c2a98a5da5a2383 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
func sum(nums []int) int {
s := 0
for _, n := range nums {
s += n
}
return s
}
func score(line []int) int {
result := 0
// first 9 frames
for i := 1; i <= 9; i++ {
// strike
if line[0] == 10 {
result += sum(line[:3])
line = line[1:]
continue
}
// spare
if line[0]+line[1] == 10 {
result += sum(line[:3])
line = line[2:]
continue
}
// normal
result += sum(line[:2])
line = line[2:]
}
// last frame
result += sum(line)
return result
}
func main() {
fmt.Println(score([]int{10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}))
fmt.Println(score([]int{9, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9, 0}))
fmt.Println(score([]int{5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment