-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
217 lines (180 loc) · 4.93 KB
/
main.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"sort"
"strings"
)
func ModelComputerClub(inputFile string) string {
file, err := os.Open(inputFile)
if err != nil {
log.Fatal("can't open file", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
//scanner.Split(bufio.ScanLines)
var numtables int
if scanner.Scan() {
n, _ := fmt.Sscanf(scanner.Text(), "%d", &numtables)
if n == 0 {
return scanner.Text()
}
} else {
log.Fatal("Number of tables not provided")
}
var openingHours, openingMinutes, closingHours, closingMinutes, openingTime, closingTime int
if scanner.Scan() {
re, err := regexp.Compile("^([0-1][0-9]|2[0-3]):[0-5][0-9] ([0-1][0-9]|2[0-3]):[0-5][0-9]$")
if err != nil {
log.Fatal(err)
}
if !re.MatchString(scanner.Text()) {
return scanner.Text()
}
fmt.Sscanf(scanner.Text(), "%d:%d %d:%d", &openingHours, &openingMinutes, &closingHours, &closingMinutes)
openingTime = openingHours*60 + openingMinutes
closingTime = closingHours*60 + closingMinutes
} else {
log.Fatal("failed to scan working hours", scanner.Text())
}
var cost int
if scanner.Scan() {
n, _ := fmt.Sscanf(scanner.Text(), "%d", &cost)
if n == 0 {
return scanner.Text()
}
} else {
log.Fatal("failed to scan cost", scanner.Text())
}
clients := make(map[string]int)
queue := ClientQueue{make([]string, 0, 1), 0}
computers := make([]Computer, numtables)
freeTables := numtables
for ind := range computers {
computers[ind] = Computer{
isOccupied: false,
user: "",
since: 0,
cost: cost,
}
}
var result strings.Builder
result.WriteString(fmt.Sprintf("%02d:%02d\n", openingHours, openingMinutes))
var hours, minutes, eventId int
var body string
for scanner.Scan() {
re, err := regexp.Compile("^([0-1][0-9]|2[0-3]):[0-5][0-9] ([13-4] [a-z1-9_-]+|2 [a-z1-9_-]+ [0-9]+)$")
if err != nil {
log.Fatal(err)
}
if !re.MatchString(scanner.Text()) {
return scanner.Text()
}
fmt.Sscanf(scanner.Text(), "%d:%d %d %s", &hours, &minutes, &eventId, &body)
result.WriteString(scanner.Text())
result.WriteString("\n")
now := hours*60 + minutes
switch eventId {
case 1:
if _, ok := clients[body]; ok {
result.WriteString(fmt.Sprintf("%02d:%02d 13 YouShallNotPass\n", hours, minutes))
continue
}
if now < openingTime || now > closingTime { //TODO: implement for working hours like 06:00-01:00
result.WriteString(fmt.Sprintf("%02d:%02d 13 NotOpenYet\n", hours, minutes))
continue
}
clients[body] = 0
queue.Push(body)
case 2:
var user string
var pc int
fmt.Sscanf(scanner.Text(), "%d:%d %d %s %d", &hours, &minutes, &eventId, &user, &pc)
if pc < 1 || pc > numtables {
log.Fatal("pc out of tables range", scanner.Text())
}
if computers[pc-1].isOccupied {
result.WriteString(fmt.Sprintf("%02d:%02d 13 PlaceIsBusy\n", hours, minutes))
continue
}
if _, ok := clients[user]; !ok {
result.WriteString(fmt.Sprintf("%02d:%02d 13 ClientUnknown\n", hours, minutes))
continue
}
if clients[user] != 0 {
computers[clients[user]-1].Free(now)
} else {
queue.Remove(user)
}
clients[user] = pc
computers[pc-1].Occupy(user, now)
freeTables--
case 3:
if freeTables > 0 {
result.WriteString(fmt.Sprintf("%02d:%02d 13 ICanWaitNoLonger!\n", hours, minutes))
continue
}
if queue.size > numtables {
queue.Remove(body)
delete(clients, body)
result.WriteString(fmt.Sprintf("%02d:%02d 11 %s", hours, minutes, body))
continue
}
case 4:
if _, ok := clients[body]; !ok {
result.WriteString(fmt.Sprintf("%02d:%02d 13 ClientUnknown\n", hours, minutes))
continue
}
freeTables++
if clients[body] != 0 {
computers[clients[body]-1].Free(now)
var client string
if !queue.IsEmpty() {
client, err = queue.Pop()
computers[clients[body]-1].Occupy(client, now)
clients[client] = clients[body]
result.WriteString(fmt.Sprintf("%02d:%02d 12 %s %d\n", hours, minutes, client, clients[body]))
freeTables--
}
if err != nil {
log.Fatal(err)
}
}
delete(clients, body)
default:
log.Fatal("event id not found", scanner.Text())
}
}
i := 0
keys := make([]string, len(clients))
for k := range clients {
keys[i] = k
i++
}
sort.Strings(keys)
for _, user := range keys {
freeTables++
if clients[user] != 0 {
computers[clients[user]-1].Free(closingTime)
}
result.WriteString(fmt.Sprintf("%02d:%02d 11 %s \n", closingHours, closingMinutes, user))
delete(clients, body)
}
result.WriteString(fmt.Sprintf("%02d:%02d\n", closingHours, closingMinutes))
for ind, pc := range computers {
hours := pc.totaltime / 60
minutes := pc.totaltime % 60
result.WriteString(fmt.Sprintf("%d %d %02d:%02d\n", ind+1, pc.revenue, hours, minutes))
}
return result.String()
}
func main() {
if len(os.Args) == 1 {
log.Fatal("Error: no file provided")
}
result := ModelComputerClub(os.Args[1])
fmt.Print(result)
}