This commit is contained in:
JOLIMAITRE Matthieu 2024-03-25 10:47:49 +01:00
parent 7f1fad4403
commit a94776695a
5 changed files with 89 additions and 0 deletions

49
pfes/sort/main.ts Executable file
View file

@ -0,0 +1,49 @@
#!/bin/env -S deno run -A
function main() {
const input = read_input("./input.csv");
const MAX_IN_GROUP = 4;
const groups = Array.from(range(1, 7))
.map((sujet_no) => ({ sujet_no, members: [] as { login: string; prio: number }[] }));
for (const prio of range(1, 7)) {
for (const entry of input) {
if (entry.placed) continue;
for (const [index, choice] of enumerate(entry.choices)) {
if (choice !== prio) continue;
if (groups[index].members.length >= MAX_IN_GROUP) continue;
groups[index].members.push({ login: entry.login, prio });
entry.placed = true;
}
}
}
let result = "";
for (const group of groups) {
const members = group.members.map((item) => `${item.login}(${item.prio})`).join("\t");
const line = `Sujet ${group.sujet_no}\t${members}`;
result += line + "\n";
}
Deno.writeTextFileSync("./output.csv", result);
}
function read_input(path: string) {
const content = Deno.readTextFileSync(path);
const cells = content.split("\n").filter((line) => line !== "").map((line) => line.split("\t"));
const toint = (n: string) => ((parsed) => isNaN(parsed) ? 0 : parsed)(parseInt(n));
return cells.map((
[login, s1, s2, s3, s4, s5, s6],
) => ({ login, choices: ([s1, s2, s3, s4, s5, s6] as const).map(toint), placed: false }));
}
function* range(from: number, to: number) {
while (from < to) yield from++;
}
function* enumerate<T>(iterable: Iterable<T>) {
let index = 0;
for (const item of iterable) yield [index++, item] as const;
}
if (import.meta.main) main();