Skip to content

Instantly share code, notes, and snippets.

@horsdal
Last active December 14, 2015 22:09
Show Gist options
  • Save horsdal/5156513 to your computer and use it in GitHub Desktop.
Save horsdal/5156513 to your computer and use it in GitHub Desktop.
public class CoursePlan
{
private readonly 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(courses.Where(course => course.IsMorning), 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.IsFull)
.FirstOrDefault()
.Register(registrant);
}
private bool OutOfInstructors()
{
return courses.Count(course => course.IsEmpty) == instructorCount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment