PR3L-TestatSystem-Abgabe1/Testat-Planer.zig

126 lines
3.0 KiB
Zig

const std = @import("std");
const Timestamp = struct {
hours: u64,
minutes: u64,
};
const testees:[]const []const u8 = &[_][] const u8 {
"Gruppe A",
"Gruppe B",
"Gruppe C",
"Gruppe D",
"Gruppe E",
"Gruppe F",
"Gruppe G",
"Gruppe H",
"Gruppe I",
"Gruppe J",
};
const number_of_testees: u64 = testees.len;
const total_minutes: u64 = 190;
const pause_minutes: u64 = 5;
pub fn main () !void {
var time = Timestamp {
.hours = 12,
.minutes = 0,
};
try printPlan(&time);
}
//? Printet timestamp im Format HH:MM
pub fn printTimestamp(ts: *Timestamp) !void {
const writer = std.io.getStdOut().writer();
try writer.print("{0d:0>2}:{1d:0>2}", .{ ts.hours, ts.minutes });
}
//? Add a given minute amount to the given timestamp
pub fn addMinutesToTimestamp(ts: *Timestamp, min: u16) void{
var addHours: u64 = min / 60;
var addMinutes: u64 = min%60;
if ((addMinutes + ts.minutes) >= 60){
addMinutes = (addMinutes + ts.minutes)%60;
addHours += 1;
ts.minutes = 0;
}
ts.hours += addHours;
ts.minutes += addMinutes;
}
//? Print the whole test-plan
pub fn printPlan(ts: *Timestamp) !void {
const rand = std.crypto.random;
const writer = std.io.getStdOut().writer();
//? BLOCK CALCULATION
const testTime: u64 = total_minutes - ((number_of_testees - 1) * pause_minutes);
const timePerTest: u64 = testTime / number_of_testees;
//? CREATE RANDOM ORDER ARRAY
const noneNumber:u64 = number_of_testees+1;
var testees_order: [number_of_testees]u64 = undefined;
//? Fill order array with noneNumber
for (testees_order, 0..) |_, index| {
testees_order[index] = noneNumber;
}
//? Fill order array with order until full
for (testees, 0..) |_, blockIndex| {
while(true) {
const randIndex:u64 = rand.intRangeLessThan(u64, 0, number_of_testees);
if (arrayContains(&testees_order, randIndex)) {
continue;
}
testees_order[blockIndex] = randIndex;
break;
}
}
//? PLAN OUTPUT
try writer.print("{d} Gruppen x {d} Minuten ({d} Minuten Pause dazwischen) \n", .{number_of_testees, timePerTest, pause_minutes});
try writer.print("Beginn des Testat-Termins: ", .{});
try printTimestamp(ts);
try writer.print("\n", .{});
try writer.print("Testat-Bloecke: \n", .{});
for (testees_order, 0..) |testee, index| {
try writer.print("{d}.) ", .{index+1});
try printTimestamp(ts);
try writer.print("-", .{});
addMinutesToTimestamp(ts, timePerTest);
try printTimestamp(ts);
addMinutesToTimestamp(ts, pause_minutes);
try writer.print(": {s} \n", .{testees[testee]});
}
}
//? Check if a given array contains a given element
pub fn arrayContains(arr: []u64, q: u64) bool {
for (arr) |element| {
if (element == q) {
return true;
}
}
return false;
}