This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
99 lines (85 loc) · 1.94 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
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
)
func parseFile(path string) (Context, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return Context{}, err
}
// nbLib nbBooks maxDays
// bookValue1 bookValue2 bookValue3
// nbBookLib1 signupCostLib1 bookShipPerDayLib1
// 2
// 2
// ...
lines := strings.Split(string(b), "\n")
line0Arr := strings.Split(lines[0], " ")
nbLib, err := strconv.Atoi(line0Arr[1])
if err != nil {
return Context{}, err
}
nbBooks, err := strconv.Atoi(line0Arr[0])
if err != nil {
return Context{}, err
}
maxDays, err := strconv.Atoi(line0Arr[2])
if err != nil {
return Context{}, err
}
// maxDays = maxDays + 1
bookScores := strings.Split(lines[1], " ")
for i := range bookScores {
if bookScores[i] == "" {
return Context{}, fmt.Errorf("i:%d contains ''", i)
}
}
libraries := make([]Library, nbLib)
for iLib := 0; iLib < len(lines[2:])/2; iLib++ {
if lines[2+(2*iLib)] == "" {
if optEnableWarnings {
fmt.Printf("WARN: empty line %d\n", 2+(2*iLib))
}
continue
}
curLib := Library{}
booksArr := strings.Split(lines[2+(2*iLib)+1], " ")
libInfosArr := strings.Split(lines[2+(2*iLib)], " ")
nbBooks, err = strconv.Atoi(libInfosArr[0])
if err != nil {
return Context{}, err
}
curLib.SignupTime, err = strconv.Atoi(libInfosArr[1])
if err != nil {
return Context{}, err
}
curLib.BooksPerDay, err = strconv.Atoi(libInfosArr[2])
if err != nil {
return Context{}, err
}
curLib.Books = make([]Book, nbBooks)
for iBook := 0; iBook < nbBooks; iBook++ {
id, err := strconv.Atoi(booksArr[iBook])
if err != nil {
return Context{}, err
}
score, err := strconv.Atoi(bookScores[id])
if err != nil {
return Context{}, err
}
curLib.Books[iBook] = Book{
ID: id,
Score: score,
}
}
curLib.ID = iLib
libraries[iLib] = curLib
}
return Context{
Libraries: libraries,
DayMax: maxDays,
}, nil
}