-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path584.go
More file actions
79 lines (70 loc) · 1.38 KB
/
584.go
File metadata and controls
79 lines (70 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// UVa 584 - Bowling
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
const plays = 10
type frame struct {
scores []int
strike, spare bool
}
func calc(frames []frame) int {
var score int
for i := 0; i < plays; i++ {
score += frames[i].scores[0]
if !frames[i].strike {
score += frames[i].scores[1]
}
if frames[i].spare || frames[i].strike {
score += frames[i+1].scores[0]
if frames[i].strike {
if frames[i+1].strike {
score += frames[i+2].scores[0]
} else {
score += frames[i+1].scores[1]
}
}
}
}
return score
}
func solve(line string) int {
var idx, s int
frames := make([]frame, plays+2)
for _, score := range strings.Split(line, " ") {
switch score {
case "X":
frames[idx].scores = []int{10}
frames[idx].strike = true
idx++
case "/":
frames[idx].scores = append(frames[idx].scores, 10-frames[idx].scores[0])
frames[idx].spare = true
idx++
default:
fmt.Sscanf(score, "%d", &s)
if frames[idx].scores = append(frames[idx].scores, s); len(frames[idx].scores) == 2 {
idx++
}
}
}
return calc(frames)
}
func main() {
in, _ := os.Open("584.in")
defer in.Close()
out, _ := os.Create("584.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var line string
for s.Scan() {
if line = s.Text(); line == "Game Over" {
break
}
fmt.Fprintln(out, solve(line))
}
}