-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerator.cs
More file actions
149 lines (127 loc) · 5.05 KB
/
Copy pathGenerator.cs
File metadata and controls
149 lines (127 loc) · 5.05 KB
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
using System.Globalization;
using System.Text.Json;
using Cocona;
using CsvHelper;
using CsvHelper.Configuration;
using Scheduler.Models;
namespace Scheduler;
internal class Generator
{
private static readonly string[] Colors = ["green", "blue", "yellow", "darkblue", "purple", "red", "orange", "brown", "gray"];
public void Run(
[Option('i', Description = "Input CSV file path")]
string csvPath,
[Option('s', Description = "Semester (e.g., winter)")]
string semester,
[Option('y', Description = "Year (e.g., 2025)")]
int year,
[Option("free-days", Description = "Target number of free days per week (optional)")]
int freeDaysTarget = 0
)
{
var csvEntries = LoadCsvEntries(csvPath);
var subjectsByCode = GroupSubjectsByCode(csvEntries);
var colorMap = AssignColors(subjectsByCode.Keys.ToList());
var allSchedules = GenerateSchedules(subjectsByCode.Values.ToList(), 0, new List<CsvEntry>());
allSchedules = ApplyFreeDaysTarget(allSchedules, freeDaysTarget);
WriteSchedulesToJson(allSchedules, semester, year, colorMap);
Console.WriteLine(allSchedules.Count == 0
? "There are 0 possible combinations"
: $"{allSchedules.Count} Schedule{(allSchedules.Count > 1 ? "s" : "")} generated");
}
private static List<CsvEntry> LoadCsvEntries(string csvPath)
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = true,
IgnoreBlankLines = true,
TrimOptions = TrimOptions.Trim
};
using var reader = new StreamReader(csvPath);
using var csv = new CsvReader(reader, config);
csv.Context.RegisterClassMap<CsvEntryMap>();
return csv.GetRecords<CsvEntry>().ToList();
}
private static Dictionary<string, List<CsvEntry>> GroupSubjectsByCode(List<CsvEntry> csvEntries)
{
return csvEntries.GroupBy(e => e.Code)
.ToDictionary(g => g.Key, g => g.ToList());
}
private static Dictionary<string, string> AssignColors(List<string> subjectCodes)
{
var colorMap = new Dictionary<string, string>();
for (var i = 0; i < subjectCodes.Count; i++)
{
colorMap[subjectCodes[i]] = Colors[i % Colors.Length];
}
return colorMap;
}
private static List<List<CsvEntry>> ApplyFreeDaysTarget(List<List<CsvEntry>> schedules, int freeDaysTarget)
{
if (freeDaysTarget <= 0) return schedules;
return schedules.Where(schedule =>
5 - schedule.Select(c => c.Day).Distinct().Count() >= freeDaysTarget
).ToList();
}
private static List<List<CsvEntry>> GenerateSchedules(List<List<CsvEntry>> subjectGroups, int index, List<CsvEntry> current)
{
var result = new List<List<CsvEntry>>();
if (index >= subjectGroups.Count)
{
result.Add([..current]);
return result;
}
foreach (var entry in subjectGroups[index].Where(entry => !HasCollision(current, entry)))
{
current.Add(entry);
result.AddRange(GenerateSchedules(subjectGroups, index + 1, current));
current.RemoveAt(current.Count - 1);
}
return result;
}
private static bool HasCollision(List<CsvEntry> current, CsvEntry candidate)
{
return current.Any(c => c.Day == candidate.Day && c.To > candidate.From && candidate.To > c.From);
}
private static void WriteSchedulesToJson(
List<List<CsvEntry>> allSchedules,
string semester,
int year,
Dictionary<string, string> colorMap
)
{
if (Directory.Exists("output")) Directory.Delete("output", true);
Directory.CreateDirectory("output");
var random = new Random();
var jsonOptions = new JsonSerializerOptions
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
IncludeFields = true
};
var fileIndex = 1;
foreach (var outputJson in allSchedules.Select(schedule => schedule.Select(e => new CustomEntry
{
Id = "CUST_" + random.Next(1000000000, int.MaxValue),
Name = e.Title,
Link = $"{e.Code}-{e.Title}",
Day = (int)e.Day,
Week = e.Weeks,
From = e.From,
To = e.To,
Rooms = [e.Room],
CustomColor = colorMap[e.Code]
}).ToList()).Select(customEntries => new OutputJson
{
Sem = semester,
Year = year,
Custom = customEntries,
Selected = customEntries.Where(c => c.Selected).Select(c => c.Id).ToList(),
Studies = []
}))
{
File.WriteAllText($"output/schedule_{fileIndex}.json", JsonSerializer.Serialize(outputJson, jsonOptions));
fileIndex++;
}
}
}