Skip to content

Instantly share code, notes, and snippets.

@horsdal
Last active December 14, 2015 15:19
Show Gist options
  • Save horsdal/5107341 to your computer and use it in GitHub Desktop.
Save horsdal/5107341 to your computer and use it in GitHub Desktop.
public class CoursePlan
{
private IEnumerable<Course> courses;
private IEnumerable<Registrant> registrants;
private readonly int instructorCount;
public CoursePlan(IEnumerable<Course> courses, IEnumerable<Registrant> registrants, int instructorCount)
{
this.courses = courses;
this.registrants = registrants;
this.instructorCount = instructorCount;
}
public Solution Solve()
{
registrants.ToList().ForEach(SolveFor);
return new Solution(new CourseList(courses.Where(course => course.IsMorning)),
new CourseList(courses.Where(course => !course.IsMorning)));
}
private void SolveFor(Registrant registrant)
{
SolveMorning(registrant);
SolveAfternoon(registrant);
}
private void SolveMorning(Registrant registrant)
{
HalfDaySolution(registrant, true);
}
private void SolveAfternoon(Registrant registrant)
{
HalfDaySolution(registrant, false);
}
private void HalfDaySolution(Registrant registrant, bool isMorning)
{
if (!OutOfInstructors())
registrant
.CourseWishes
.Where(course => course.IsMorning == isMorning)
.Where(course => course.Template.Limit < course.Registrants.Count)
.FirstOrDefault()
.Registrants.Add(registrant);
}
private bool OutOfInstructors()
{
return courses.Count(course => course.Registrants.Count == 0) == instructorCount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment