-
Notifications
You must be signed in to change notification settings - Fork 0
/
WayneTable.cs
215 lines (193 loc) · 7.51 KB
/
WayneTable.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DrWayne {
public class WayneTable {
public WayneTable(List<Doctor> doctorList, int year, int month) {
_wayneTable = new List<Wayne>();
_doctorList = doctorList;
if (month < 1 || month > 12)
throw new Exception("Month must be 1-12");
Month = month;
Year = year;
_totalDaysInMonth = DateTime.DaysInMonth(year, month);
_totalWorkingDay = Enumerable.Range(1, _totalDaysInMonth)
.Sum(i => new DateTime(year, month, i).IsHoliday() ? 0 : 1);
}
private List<Wayne> _wayneTable;
private List<Doctor> _doctorList;
public int Month { get; private set; }
public int Year { get; private set; }
private int _totalDaysInMonth;
private int _totalWorkingDay;
private bool AddWayneIfAcceptable(Wayne wayne) {
if (wayne.IsAcceptable()) {
_wayneTable.Add(wayne);
return true;
}
return false;
}
public void AddFixWayne(Wayne wayne) {
if (!wayne.IsAcceptable()) {
Console.WriteLine(wayne.ToString());
throw new Exception("Your fixed wayne is not correct, please recheck it");
}
_doctorList.First(x => x == wayne.ERDoctor).ERWayne.Add(wayne.WayneDate);
_doctorList.First(x => x == wayne.WardDoctor).WardWayne.Add(wayne.WayneDate);
if (wayne.WayneDate.NeedOPD())
_doctorList.First(x => x == wayne.OPDDoctor).OPDWayne.Add(wayne.WayneDate);
_wayneTable.Add(wayne);
}
private WayneTable Copy() {
var wayneTableCopy = new WayneTable(_doctorList, Year, Month);
wayneTableCopy._wayneTable = this._wayneTable.ToList();
return wayneTableCopy;
}
private bool IsDone() {
return _wayneTable.Count == _totalDaysInMonth;
}
private int GetFirstDayToFill() {
return Enumerable.Range(1, _totalDaysInMonth)
.Except(_wayneTable.Select(x => x.WayneDate.Day).OrderBy(x => x))
.Min();
}
private bool FairEnough() {
//if (//// Special case for each month
//_doctorList.Single(d => d.Name == "P' Did").GetAllWayneDate().Count() == 10 &&
// gofilm.ERWayne.Count() == 5 &&
// gofilm.WardWayne.Count() == 3 &&
// saran.GetAllWayneDate().Count() == 9 &&
// saran.ERWayne.Count() == 5 &&
// saran.WardWayne.Count() == 3 &&
//// Regular cases
//doctorListWithoutHandicap.Max(x => x.ERWayne.Count()) - doctorListWithoutHandicap.Min(x => x.ERWayne.Count()) <= 1 &&
//doctorListWithoutHandicap.Max(x => x.WardWayne.Count()) - doctorListWithoutHandicap.Min(x => x.WardWayne.Count()) <= 1// &&
//restList.Max(x => x.RestOnHolidayCount) - restList.Min(x => x.RestOnHolidayCount) <= 1 &&
//restList.Max(x => x.RestOnWorkingDayCount) - restList.Min(x => x.RestOnWorkingDayCount) <= 1
//) {
return true;
//}
//return false;
}
public void Fill() {
if (IsDone()) {
ShowFairness();
if (!FairEnough()) return;
ShowResult();
return;
}
var currentDate = new DateTime(Year, Month, GetFirstDayToFill());
var erTireness = 10;
var wardTireness = 5;
var OPDTireness = 2;
if (currentDate.IsHoliday()) {
erTireness = (erTireness * 3) / 2;
wardTireness = (wardTireness * 3) / 2;
}
var wayne = new Wayne(currentDate);
var rnd = new Random();
var doctorListCopy = _doctorList
.Where(x => !x.AbsenceList.Contains(currentDate))
.Where(x => !x.AmInWayneMoreThanTwoConsecutiveDays(currentDate))
.OrderBy(x => (x.ERWayne.Count() + x.WardWayne.Count() + x.OPDWayne.Count()) / x.Factor)
.ThenBy(x => x.Tireness / x.Factor)
.ThenBy(x => rnd.Next())
.Take(currentDate.NeedOPD() ? 4 : 3)
.ToList();
foreach (var erDoctor in doctorListCopy.Where(d => d.AmIOKForERThisDay(currentDate))) {
wayne.ERDoctor = erDoctor;
erDoctor.ERWayne.Add(currentDate);
erDoctor.Tireness += erTireness;
foreach (var wardDoctor in doctorListCopy.Where(d => d != erDoctor && d.AmIOKForWardThisDay(currentDate))) {
wayne.WardDoctor = wardDoctor;
wardDoctor.WardWayne.Add(currentDate);
wardDoctor.Tireness += wardTireness;
if (!currentDate.NeedOPD()) {
var wayneTableCopy = Copy();
if (wayneTableCopy.AddWayneIfAcceptable(wayne)) {
wayneTableCopy.Fill();
}
}
else {
foreach (var OPDDoctor in doctorListCopy.Where(d => d != erDoctor && d != wardDoctor)) {
wayne.OPDDoctor = OPDDoctor;
OPDDoctor.OPDWayne.Add(currentDate);
OPDDoctor.Tireness += OPDTireness;
var wayneTableCopy = Copy();
if (wayneTableCopy.AddWayneIfAcceptable(wayne)) {
wayneTableCopy.Fill();
}
OPDDoctor.Tireness -= OPDTireness;
OPDDoctor.OPDWayne.Remove(currentDate);
}
}
wardDoctor.Tireness -= wardTireness;
wardDoctor.WardWayne.Remove(currentDate);
}
erDoctor.Tireness -= erTireness;
erDoctor.ERWayne.Remove(currentDate);
}
}
private void ShowFairness() {
var doctorListWithoutHandicap = _doctorList.Where(d => d.Factor == 1);
var restList = doctorListWithoutHandicap
.Select(x => new {
Name = x.Name,
WorkDayList = x.GetAllWayneDate()
})
.Select(x => new {
Name = x.Name,
RestOnWorkingDayCount = _totalWorkingDay - x.WorkDayList.Count(y => !y.IsHoliday()),
RestOnHolidayCount = (_totalDaysInMonth - _totalWorkingDay) - x.WorkDayList.Count(y => y.IsHoliday())
}).ToList();
Console.WriteLine();
Console.WriteLine("{0, -8} : {1, 10} {2, 10} {3, 10}", "Name", "RestWDay", "RestHoliday", "Sum");
foreach (var p in restList) {
Console.WriteLine("{0, -8} : {1, 10} {2, 10} {3, 10}", p.Name,
p.RestOnWorkingDayCount,
p.RestOnHolidayCount,
p.RestOnHolidayCount + p.RestOnWorkingDayCount);
}
}
public override string ToString() {
var sb = new StringBuilder();
sb.AppendFormat("Wayne of Month {0}, {1}\n", Month, Year);
sb.AppendFormat("{0, -7} {1, 10} {2, 10} {3, 10}\n", "Date", "ER", "WARD", "OPD");
foreach (var wayne in _wayneTable.OrderBy(x => x.WayneDate)) {
sb.AppendFormat("{0, -7} {1, 10} {2, 10} {3, 10}\n",
wayne.WayneDate.ToString("ddd dd"),
wayne.ERDoctor.Name,
wayne.WardDoctor.Name,
wayne.OPDDoctor == null ? "-" : wayne.OPDDoctor.Name);
}
return sb.ToString();
}
public string ToCsvFormat() {
var sb = new StringBuilder();
sb.AppendLine(string.Join(",", new [] { "Date" }.Concat(_doctorList.Select(x => x.Name))));
foreach (var wayne in _wayneTable.OrderBy(x => x.WayneDate)) {
sb.AppendLine(string.Join(",", new [] {wayne.WayneDate.ToString("ddd dd")}.Concat(_doctorList.Select(x => x.ERWayne.Contains(wayne.WayneDate)
? "ER"
: x.WardWayne.Contains(wayne.WayneDate)
? "WA"
: x.OPDWayne.Contains(wayne.WayneDate)
? "OPD"
: "-"))));
}
return sb.ToString();
}
public void ShowResult() {
Console.WriteLine("{0, -10} : {1, 5} {2, 5} {3, 5}", "Name", "ER", "Ward", "OPD");
foreach (var p in _doctorList) {
Console.WriteLine("{0, -10} : {1, 5} {2, 5} {3, 5}", p.Name, p.ERWayne.Count(), p.WardWayne.Count(), p.OPDWayne.Count());
}
Console.WriteLine("{0}", ToCsvFormat());
//Console.WriteLine("{0}", ToString());
//Console.WriteLine("Tireness level at the end of the month");
//Console.WriteLine(string.Join("\n", _doctorList.Select(x => x.Name + " : " + x.Tireness)));
//Console.WriteLine("=== END ===\n");
Console.ReadLine();
}
}
}