-
Notifications
You must be signed in to change notification settings - Fork 0
/
slots.go
180 lines (156 loc) · 4.69 KB
/
slots.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
package plexams
import (
"fmt"
"sort"
"time"
set "github.com/deckarep/golang-set/v2"
"github.com/obcode/plexams.go/graph/model"
)
type Slot []int
func PrintGOSlots(semesterConfigSlots []*model.Slot, goSlots [][]int) error {
// fmt.Printf("GO Slots: %v\n", p.goSlots())
// fmt.Printf("all Slots: %v\n", p.semesterConfig.Slots)
// fixedDay := time.Date(2023, 1, 31, 0, 0, 0, 0, time.Local)
// fixedTime := time.Date(2023, 1, 31, 12, 30, 0, 0, time.Local)
isGo := false
allowedSlots := CalculatedAllowedSlots(semesterConfigSlots, goSlots, isGo, &model.Constraints{
ExcludeDays: []*time.Time{},
PossibleDays: []*time.Time{},
FixedDay: nil, // &fixedDay,
FixedTime: nil, // &fixedTime,
})
fmt.Println("AllowedSlots\n============")
for _, slot := range allowedSlots {
fmt.Printf("%v\n", slot)
}
return nil
}
func allSlots(semesterConfigSlots []*model.Slot, allowed bool) map[int]map[int]bool {
allSlots := make(map[int]map[int]bool)
for _, slot := range semesterConfigSlots {
dayMapAllowed, ok := allSlots[slot.DayNumber]
if !ok {
dayMapAllowed = make(map[int]bool)
}
dayMapAllowed[slot.SlotNumber] = allowed
allSlots[slot.DayNumber] = dayMapAllowed
}
return allSlots
}
func CalculatedAllowedSlots(semesterConfigSlots []*model.Slot, goSlots [][]int, isGO bool, constraints *model.Constraints) []*model.Slot {
allSlotsAllowed := allSlots(semesterConfigSlots, true)
noSlotsAllowed := allSlots(semesterConfigSlots, false)
var slotsMap map[int]map[int]bool
if isGO {
for _, slot := range goSlots {
dayMap, ok := noSlotsAllowed[slot[0]]
if ok {
_, ok := dayMap[slot[1]]
if ok {
noSlotsAllowed[slot[0]][slot[1]] = true
}
}
}
slotsMap = noSlotsAllowed
} else {
slotsMap = allSlotsAllowed
}
slots := slotsToModelSlots(semesterConfigSlots, slotsMap)
if constraints != nil {
if constraints.ExcludeDays != nil && len(constraints.ExcludeDays) > 0 {
slotsWithoutExcludedDays := make([]*model.Slot, 0)
for _, excludeDay := range constraints.ExcludeDays {
for _, slot := range slots {
s := slot.Starttime.Local()
e := excludeDay.Local()
if constraints.Ancode == 204 {
fmt.Printf("slot %s -- excluded day %s\n", s.String(), e.String())
}
if e.Year() == s.Year() && e.Month() == s.Month() && e.Day() == s.Day() {
fmt.Println(">>>> FOUND <<<<")
break
}
slotsWithoutExcludedDays = append(slotsWithoutExcludedDays, slot)
}
}
slots = slotsWithoutExcludedDays
}
if constraints.PossibleDays != nil && len(constraints.PossibleDays) > 0 {
slotsWithIncludedDays := make([]*model.Slot, 0)
for _, includeDay := range constraints.PossibleDays {
for _, slot := range slots {
if time.Date(slot.Starttime.Year(), slot.Starttime.Month(), slot.Starttime.Day(), 0, 0, 0, 0, time.Local).
Equal(*includeDay) {
slotsWithIncludedDays = append(slotsWithIncludedDays, slot)
}
}
}
slots = slotsWithIncludedDays
}
if constraints.FixedDay != nil {
allowed := make([]*model.Slot, 0)
for _, slot := range slots {
if time.Date(slot.Starttime.Year(), slot.Starttime.Month(), slot.Starttime.Day(), 0, 0, 0, 0, time.Local).
Equal(*constraints.FixedDay) {
allowed = append(allowed, slot)
}
}
slots = allowed
}
if constraints.FixedTime != nil {
allowed := []*model.Slot{}
for _, slot := range slots {
if slot.Starttime.Equal(*constraints.FixedTime) {
allowed = []*model.Slot{slot}
break
}
}
slots = allowed
}
}
return slots
}
func slotsToModelSlots(semesterConfigSlots []*model.Slot, slots map[int]map[int]bool) []*model.Slot {
modelSlots := make([]*model.Slot, 0)
for _, slot := range semesterConfigSlots {
if slots[slot.DayNumber][slot.SlotNumber] {
modelSlots = append(modelSlots, slot)
}
}
return modelSlots
}
func mergeAllowedSlots(sliceOfSlots [][]*model.Slot) []*model.Slot {
slotsRes := set.NewSet[*model.Slot]()
for i, slots := range sliceOfSlots {
slotsSet := set.NewSet[*model.Slot]()
for _, slot := range slots {
slotsSet.Add(slot)
}
if i == 0 {
slotsRes = slotsSet
} else {
slotsRes = slotsRes.Intersect(slotsSet)
}
}
slots := make([]*model.Slot, 0)
for slot := range slotsRes.Iter() {
slots = append(slots, slot)
}
return sortSlots(slots)
}
func sortSlots(slots []*model.Slot) []*model.Slot {
slotMap := make(map[int]*model.Slot)
keys := make([]int, 0)
for _, slot := range slots {
// assume there are not more than 9 Slots
key := slot.DayNumber*10 + slot.SlotNumber
slotMap[key] = slot
keys = append(keys, key)
}
sort.Ints(keys)
sortedSlots := make([]*model.Slot, 0, len(slots))
for _, key := range keys {
sortedSlots = append(sortedSlots, slotMap[key])
}
return sortedSlots
}