-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoursePlanner.php
executable file
·230 lines (205 loc) · 7.55 KB
/
CoursePlanner.php
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
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
declare (strict_types = 1);
require_once 'Course.php';
class CoursePlanner
{
private $courseList;
private $allPlans;
private $plan;
private $unwantedHours;
private $conflict = -1;
private $year;
private $semester;
const DEFAULT_DAYS = ['M', 'T', 'W', 'Th', 'F', 'Sa'];
public function __construct(int $year = null, int $semester = null)
{
$this->courseList = [];
$this->allPlans = [];
$this->plan = [];
$this->unwantedHours = [];
if (!is_null($year) && !is_null($semester)) {
$this->year = (int) str_pad((String) $year, 4, '20', STR_PAD_LEFT);
$this->semester = $semester;
} else {
$m = (int) date('m');
$s = 1;
if ($m >= 1 && $m <= 5) {
$s = 2;
} elseif ($m >= 6 && $m <= 7) {
$s = 3;
}
$this->year = date('Y');
if ($s !== 1) {
--$this->year;
}
$this->semester = $s;
}
}
/**
* @param string $name Course name
* @param int $code Course code
* @param array $hours [Section=>[DayHour]] formatted course hours. ex.: ["01"=>["M11","Th10","W16"], "022=>...].
* If not given, it'll be fetched.
*
* @throws Exception
*/
public function addCourse(String $name, int $code = null, array $hours = null)
{
$credit = null;
$fullNames = [];
$insts = [];
if (is_null($hours)) {
if (!is_null($code)) {
$name .= $code;
}
if (!file_exists('CourseFetcher.php')) {
throw new Exception('This option requires course fetcher to work.');
}
require_once 'CourseFetcher.php';
$courseFetcher = new CourseFetcher((int) $this->year, (int) $this->semester);
if (strpos($name, '.') !== false) {
// contains explicit section detail
list($name, $section) = explode('.', $name);
$courseDetails = $courseFetcher->getDetails($name);
if (!array_key_exists($section, $courseDetails)) {
throw new Exception($name.'.'.$section.' cannot be added; course does not have that section for the semester.');
}
$courseDetails = [$section => $courseDetails[$section]];
} else {
$courseDetails = $courseFetcher->getDetails($name);
}
if ($courseDetails === false) {
throw new Exception($name." cannot be added; if you've spelled it correctly, the course is not available for the semester.");
}
$hours = [];
$sFullNames = [];
$sInsts = [];
foreach ($courseDetails as $section => $details) {
// Set the course credit
if (empty($credit)) {
$credit = $details[4];
}
// 0 days 1 hours
$courseDays = preg_split('/([A-Z][a-z]*)/', $details[0], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
if (empty($courseDays)) {
throw new Exception($name.' has no schedule details.');
}
$courseHours = [];
if (strlen(preg_replace('![^A-Z]+!', null, $details[0])) == strlen($details[1])) {
$courseHours = str_split($details[1]);
} else {
for ($i = 0; $i < strlen($details[1]); ++$i) {
if (!empty($courseHours)) {
$el = (int) array_pop($courseHours);
if (($newEl = 10 * $el + $details[1]{ $i}) > 11) {
array_push($courseHours, $el);
array_push($courseHours, $details[1]{ $i});
} else {
array_push($courseHours, $newEl);
}
} else {
array_push($courseHours, $details[1]{ $i});
}
}
}
for ($j = 0; $j < count($courseHours); ++$j) {
$courseDays[$j] .= (int) $courseHours[$j] + 8;
$sFullNames[$j] = $details[5][$j]["fullName"];
$sInsts[$j] = $details[5][$j]["inst"];
}
$hours[] = [$section => $courseDays];
$fullNames[$section] = $sFullNames;
$insts[$section] = $sInsts;
}
}
$this->courseList[] = new Course($name, $hours, (int) $credit, $fullNames, $insts);
}
/**
* @param string $hour DayHour formatted hour. ex.: M11, F12, Th13
* If only an integer is given, all days will be automatically added.
*/
public function addUnwantedHour(String $hour)
{
if (!preg_match('#[^0-9]#i', $hour)) {
foreach (self::DEFAULT_DAYS as $day) {
$this->unwantedHours[] = $day.$hour;
}
} else {
$this->unwantedHours[] = $hour;
}
}
public function getConflict(): int
{
return $this->conflict;
}
/**
* @param int $j
* @param bool $duplicateHours Include the sections with same hours or not
*/
private function generatePlans(int $j, bool $duplicateHours = false)
{
$course = $this->courseList[$j];
$hours = $course->getHours();
if ($duplicateHours === false) {
$newHours = [];
foreach ($hours as $hourSet) {
$section = array_keys($hourSet)[0];
$newHours[$section] = $hourSet[$section];
}
$hours = [];
foreach (array_unique($newHours, SORT_REGULAR) as $section => $newHourSet) {
$hours[] = [$section => $newHourSet];
}
}
for ($i = 0; $i < count($hours); ++$i) {
array_push($this->plan, new Course($course->getName(), (array) $hours[$i], $course->getCredit(), $course->getFullName(), $course->getInst()));
if ($j !== count($this->courseList) - 1) {
$this->generatePlans($j + 1, $duplicateHours);
} elseif (!empty($this->plan)) {
$this->allPlans[] = $this->plan;
array_pop($this->plan);
}
}
array_pop($this->plan);
}
public function getAllPlans(bool $duplicateHours = false): array
{
if (empty($this->allPlans)) {
$this->generatePlans(0, $duplicateHours);
}
return $this->allPlans;
}
public function getBestPlans(bool $duplicateHours = false): array
{
$bestPlans = [];
$plans = $this->getAllPlans($duplicateHours);
for ($i = 0; $i < count($plans); ++$i) {
$hours = [];
$points = 0;
foreach ($plans[$i] as $course) {
foreach ($course->getHours() as $courseHours) {
foreach ($courseHours as $hour) {
if (array_search($hour, $this->unwantedHours) !== false) {
++$points;
}
if (array_search($hour, $hours) !== false) {
++$points;
} else {
$hours[] = $hour;
}
}
}
}
if (count($plans[$i]) == count($this->courseList)) {
$bestPlans[$points][] = $plans[$i];
}
}
ksort($bestPlans);
$this->conflict = array_keys($bestPlans)[0];
$best = array_shift($bestPlans);
while (empty($best)) {
$best = array_shift($bestPlans);
}
return $best;
}
}