Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/slow course combinations #46

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 142 additions & 46 deletions src/beans/Oscar.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,61 +73,157 @@ class Oscar {
}

getCombinations(desiredCourses, pinnedCrns, excludedCrns) {
// idea: do with a stack, then you can easily break out
const crnsList = [];
const dfs = (courseIndex = 0, crns = []) => {
const stack = [[0, []]]; // seed the stack with default values

while (stack.length > 0 && crnsList.length < 1000) {
// max number here
// if (crnsList.length >= 1000) {
// break;
// }

// process next value on stack
const [courseIndex, crns] = stack.pop();
// console.log(courseIndex, crns);

// apparently this isn't allowed because of husky linting no-continue
// if (courseIndex === desiredCourses.length) {
// crnsList.push(crns);
// // return;
// continue;
// }

if (courseIndex === desiredCourses.length) {
crnsList.push(crns);
return;
}
const course = this.findCourse(desiredCourses[courseIndex]);
const isIncluded = (section) => !excludedCrns.includes(section.crn);
const isPinned = (section) => pinnedCrns.includes(section.crn);
const hasConflict = (section) =>
[...pinnedCrns, ...crns].some((crn) =>
hasConflictBetween(this.findSection(crn), section)
);
if (course.hasLab) {
const pinnedOnlyLecture = course.onlyLectures.find(isPinned);
const pinnedOnlyLab = course.onlyLabs.find(isPinned);
const pinnedAllInOne = course.allInOnes.find(isPinned);
if ((pinnedOnlyLecture && pinnedOnlyLab) || pinnedAllInOne) {
dfs(courseIndex + 1, crns);
} else if (pinnedOnlyLecture) {
pinnedOnlyLecture.associatedLabs.filter(isIncluded).forEach((lab) => {
if (hasConflict(lab)) return;
dfs(courseIndex + 1, [...crns, lab.crn]);
});
} else if (pinnedOnlyLab) {
pinnedOnlyLab.associatedLectures
.filter(isIncluded)
.forEach((lecture) => {
} else {
const course = this.findCourse(desiredCourses[courseIndex]);
const isIncluded = (section) => !excludedCrns.includes(section.crn);
const isPinned = (section) => pinnedCrns.includes(section.crn);
const hasConflict = (section) =>
[...pinnedCrns, ...crns].some((crn) =>
hasConflictBetween(this.findSection(crn), section)
);
if (course.hasLab) {
const pinnedOnlyLecture = course.onlyLectures.find(isPinned);
const pinnedOnlyLab = course.onlyLabs.find(isPinned);
const pinnedAllInOne = course.allInOnes.find(isPinned);
if ((pinnedOnlyLecture && pinnedOnlyLab) || pinnedAllInOne) {
// dfs(courseIndex + 1, crns);
stack.push([courseIndex + 1, crns]);
} else if (pinnedOnlyLecture) {
pinnedOnlyLecture.associatedLabs
.filter(isIncluded)
.forEach((lab) => {
if (hasConflict(lab)) return;
// dfs(courseIndex + 1, [...crns, lab.crn]);
stack.push([courseIndex + 1, [...crns, lab.crn]]);
});
} else if (pinnedOnlyLab) {
pinnedOnlyLab.associatedLectures
.filter(isIncluded)
.forEach((lecture) => {
if (hasConflict(lecture)) return;
// dfs(courseIndex + 1, [...crns, lecture.crn]);
stack.push([courseIndex + 1, [...crns, lecture.crn]]);
});
} else {
course.onlyLectures.filter(isIncluded).forEach((lecture) => {
if (hasConflict(lecture)) return;
dfs(courseIndex + 1, [...crns, lecture.crn]);
lecture.associatedLabs.filter(isIncluded).forEach((lab) => {
if (hasConflict(lab)) return;
// dfs(courseIndex + 1, [...crns, lecture.crn, lab.crn]);
stack.push([courseIndex + 1, [...crns, lecture.crn, lab.crn]]);
});
});
} else {
course.onlyLectures.filter(isIncluded).forEach((lecture) => {
if (hasConflict(lecture)) return;
lecture.associatedLabs.filter(isIncluded).forEach((lab) => {
if (hasConflict(lab)) return;
dfs(courseIndex + 1, [...crns, lecture.crn, lab.crn]);
course.allInOnes.filter(isIncluded).forEach((section) => {
if (hasConflict(section)) return;
// dfs(courseIndex + 1, [...crns, section.crn]);
stack.push([courseIndex + 1, [...crns, section.crn]]);
});
});
course.allInOnes.filter(isIncluded).forEach((section) => {
if (hasConflict(section)) return;
dfs(courseIndex + 1, [...crns, section.crn]);
}
} else if (course.sections.some(isPinned)) {
// dfs(courseIndex + 1, crns);
stack.push([courseIndex + 1, crns]);
} else {
Object.values(course.sectionGroups).forEach((sectionGroup) => {
// if (crnsList.length >= 1000) {
// return;
// }

const section = sectionGroup.sections.find(isIncluded);
if (!section || hasConflict(section)) return;
// dfs(courseIndex + 1, [...crns, section.crn]);
stack.push([courseIndex + 1, [...crns, section.crn]]);
});
}
} else if (course.sections.some(isPinned)) {
dfs(courseIndex + 1, crns);
} else {
Object.values(course.sectionGroups).forEach((sectionGroup) => {
const section = sectionGroup.sections.find(isIncluded);
if (!section || hasConflict(section)) return;
dfs(courseIndex + 1, [...crns, section.crn]);
});
}
};
dfs();
}

// const dfs = (courseIndex = 0, crns = []) => {
// if (crnsList.length >= 1000) {
// return;
// }

// if (courseIndex === desiredCourses.length) {
// crnsList.push(crns);
// return;
// }
// const course = this.findCourse(desiredCourses[courseIndex]);
// const isIncluded = (section) => !excludedCrns.includes(section.crn);
// const isPinned = (section) => pinnedCrns.includes(section.crn);
// const hasConflict = (section) =>
// [...pinnedCrns, ...crns].some((crn) =>
// hasConflictBetween(this.findSection(crn), section)
// );
// if (course.hasLab) {
// const pinnedOnlyLecture = course.onlyLectures.find(isPinned);
// const pinnedOnlyLab = course.onlyLabs.find(isPinned);
// const pinnedAllInOne = course.allInOnes.find(isPinned);
// if ((pinnedOnlyLecture && pinnedOnlyLab) || pinnedAllInOne) {
// dfs(courseIndex + 1, crns);
// } else if (pinnedOnlyLecture) {
// pinnedOnlyLecture.associatedLabs.filter(isIncluded).forEach((lab) => {
// if (hasConflict(lab)) return;
// dfs(courseIndex + 1, [...crns, lab.crn]);
// });
// } else if (pinnedOnlyLab) {
// pinnedOnlyLab.associatedLectures
// .filter(isIncluded)
// .forEach((lecture) => {
// if (hasConflict(lecture)) return;
// dfs(courseIndex + 1, [...crns, lecture.crn]);
// });
// } else {
// course.onlyLectures.filter(isIncluded).forEach((lecture) => {
// if (hasConflict(lecture)) return;
// lecture.associatedLabs.filter(isIncluded).forEach((lab) => {
// if (hasConflict(lab)) return;
// dfs(courseIndex + 1, [...crns, lecture.crn, lab.crn]);
// });
// });
// course.allInOnes.filter(isIncluded).forEach((section) => {
// if (hasConflict(section)) return;
// dfs(courseIndex + 1, [...crns, section.crn]);
// });
// }
// } else if (course.sections.some(isPinned)) {
// dfs(courseIndex + 1, crns);
// } else {
// Object.values(course.sectionGroups).forEach((sectionGroup) => {
// if (crnsList.length >= 1000) {
// return;
// }

// const section = sectionGroup.sections.find(isIncluded);
// if (!section || hasConflict(section)) return;
// dfs(courseIndex + 1, [...crns, section.crn]);
// });
// }
// };
// dfs();
// console.log(crnsList);

return crnsList.map((crns) => {
const startMap = {};
const endMap = {};
Expand Down