-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
86 lines (79 loc) · 1.48 KB
/
parser.go
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
80
81
82
83
84
85
86
package fm3
import (
"strings"
"regexp"
"strconv"
"errors"
"log"
"fmt"
)
type Ptr struct {
p int
}
// Parse parses the input and parse it as a list of problems.
func Parse(lines []string) []*Prob {
p := Ptr {
p: 0,
}
var res []*Prob
for ; ; {
prob, err := parse(lines, &p);
if err != nil {
log.Fatal(err)
}
if prob == nil {
return res
}
res = append(res, prob)
}
}
func parse(lines []string, p *Ptr) (*Prob, error) {
for ; p.p < len(lines) && !strings.Contains(lines[p.p], "ばか詰"); p.p++ {
}
if p.p == len(lines) {
return nil, nil
}
stepS := regexp.MustCompile("\\d+").FindString(lines[p.p])
p.p++
step, err := strconv.Atoi(stepS)
if err != nil {
return nil, errors.New(fmt.Sprintf("step %d is not an integer.", stepS))
}
c := parseCircum(lines, p)
return &Prob {
board: c,
step: step,
}, nil
}
func parseCircum(lines []string, p *Ptr) *Circum {
edge := "+---------------------------+"
for !strings.HasPrefix(lines[p.p], edge) {
p.p++
}
p.p++
res := NewCircum()
res.Hither = true
for j := 1; j <= 9; j, p.p = j+1, p.p+1 {
row := computeRow(lines[p.p])
for i := 1; i <= 9; i++ {
res.Board[i][j] = row[i]
}
}
p.p++
// TODO: hand
return res
}
func computeRow(line string) [10]*Piece {
rs := []rune(line)
var res [10]*Piece
for i := 9; i >= 1; i-- {
p := (9 - i) * 2
if string(rs[p + 2]) != "・" {
res[i] = &Piece {
Hither: string(rs[p + 1]) == " ",
Name: fromLetter[string(rs[p + 2])],
}
}
}
return res
}