Last active
December 14, 2015 22:09
-
-
Save horsdal/5156513 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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