forked from ociule/codeeval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheverything_or_nothing.go
131 lines (118 loc) · 3.12 KB
/
everything_or_nothing.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
package main
import "fmt"
import "log"
import "bufio"
import "os"
import "strings"
type Object struct {
User int
File int
}
type Permission struct {
Grant bool
Read bool
Write bool
}
type FilePermissions struct {
Permissions map[Object]Permission
}
func InitFilePermissions() FilePermissions {
p := FilePermissions{}
p.Permissions = make(map[Object]Permission, 1000)
p.Setup(1, 1, true, true, true)
p.Setup(1, 2, true, false, true)
p.Setup(2, 1, false, true, true)
p.Setup(2, 2, false, false, true)
p.Setup(2, 3, false, true, false)
p.Setup(3, 1, true, true, false)
p.Setup(3, 2, true, false, false)
p.Setup(3, 3, true, true, false)
p.Setup(4, 1, true, false, true)
p.Setup(4, 2, true, true, true)
p.Setup(4, 3, true, false, false)
p.Setup(5, 1, false, true, true)
p.Setup(5, 3, false, false, true)
p.Setup(6, 1, false, true, false)
p.Setup(6, 2, false, false, true)
p.Setup(6, 3, false, true, true)
return p
}
func (p *FilePermissions) Setup(user, file int, grant, read, write bool) {
p.Permissions[Object{user, file}] = Permission{Grant: grant, Read: read, Write: write}
}
func checkStringPerm(perm string) bool {
return perm == "grant" || perm == "read" || perm == "write"
}
func (p *FilePermissions) CheckPerm(user, file int, perm string) bool {
// perm MUST be one of "grant", "read", "write"
if !checkStringPerm(perm) {
panic("Wrong perm")
}
permsUserOnFile := p.Permissions[Object{User: user, File: file}]
switch perm {
case "grant":
return permsUserOnFile.Grant
case "read":
return permsUserOnFile.Read
case "write":
return permsUserOnFile.Write
}
return false
}
func (p *FilePermissions) GrantWithString(user, file int, perm string) {
// perm MUST be one of "grant", "read", "write"
if !checkStringPerm(perm) {
panic("Wrong perm")
}
permsUserOnFile := p.Permissions[Object{User: user, File: file}]
switch perm {
case "grant":
permsUserOnFile.Grant = true
case "read":
permsUserOnFile.Read = true
case "write":
permsUserOnFile.Write = true
}
p.Permissions[Object{User: user, File: file}] = permsUserOnFile
}
func (p *FilePermissions) Run(in string) string {
split := strings.Fields(in)
for _, inst := range split {
user, file, perm, granted_perm, grantee := 0, 0, "", "", 0
_, _ = fmt.Sscanf(inst, "user_%d=>file_%d=>%5s=>%s", &user, &file, &perm, &granted_perm)
if !p.CheckPerm(user, file, perm) {
return "False"
}
if perm == "grant" {
splitGP := strings.Split(granted_perm, "=>user_")
granted_perm = splitGP[0]
_, _ = fmt.Sscanf(splitGP[1], "%d", &grantee)
//fmt.Println(user, file, perm, granted_perm, grantee)
p.GrantWithString(grantee, file, granted_perm)
}
//fmt.Println(user, file, perm, granted_perm, grantee)
}
return "True"
}
func (p *FilePermissions) PPrint() {
for n := 0; n < 200; n += 1 {
}
//fmt.Println(strings.Join(temp, ""))
}
func solve(in string) string {
p := InitFilePermissions()
//p.Run()
return p.Run(in)
}
func main() {
file, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
fmt.Println(solve(line))
}
}