Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. // .....
  2.  
  3. const my_courses = [
  4. courses.find(c => c.subject == 198 && c.code == 205),
  5. courses.find(c => c.subject == 198 && c.code == 211),
  6. courses.find(c => c.subject == 590 && c.code == 101),
  7. courses.find(c => c.subject == 640 && c.code == 250),
  8. ];
  9.  
  10. function* cartesian(head, ...tail) {
  11. const remainder = tail.length > 0 ? cartesian(...tail) : [[]];
  12. for (let r of remainder)
  13. for (let h of head)
  14. yield [h, ...r];
  15. }
  16.  
  17. function* permutations(a, b) {
  18. for (let i = 0; i < a.length; i++)
  19. for (let j = 0; j < b.length; j++)
  20. yield [a[i], b[j]];
  21. }
  22.  
  23. function combinations(n, lst) {
  24. if (!n) return [[]];
  25. if (!lst.length) return [];
  26.  
  27. var x = lst[0],
  28. xs = lst.slice(1);
  29.  
  30. return combinations(n - 1, xs).map(function (t) {
  31. return [x].concat(t);
  32. }).concat(combinations(n, xs));
  33. }
  34.  
  35. const times_compatible = (t0, t1) => !time.intersects(t0.interval, t1.interval) || t0.day != t1.day;
  36. const sections_compatible = (s0, s1) => Array.from(cartesian(s0.times, s1.times)).reduceRight((acc, pair) => acc && times_compatible(...pair));
  37. const schedule_compatible = sections => combinations(2, sections).reduceRight((acc, pair) => acc && sections_compatible(...pair));
  38.  
  39. const schedules = cartesian(...my_courses.map(c => c.sections));
  40.  
  41. let results = [];
  42. for (let schedule of schedules) {
  43. if (schedule_compatible(schedule))
  44. results.push(schedule);
  45. }
  46. console.log(results);
  47. console.log(`Valid: ${results.length} of ${my_courses.map(c => c.sections.length).reduceRight((acc, v) => acc * v)}`);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement