BEGIN { srand() nseats = 20 validseats = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19" print ".SH" print ".CE 1" printf("\\s+9Seating Plan\\s0\n") print ".LP" system("date '+%Y-%m-%d %H:%M'") print ".sp 2" print ".PS 6i" print "down" } { name = $1 sub(/.*\t/, "") if ($0 == "*") { $0 = validseats } if (NF == 1) { final[$0] = name } else { pers[name] = $0 npos[name] = NF rest++ } } function getrandpos(name, pos, a, x, i) { split(pos, a) while (i++ < 5*length(a)) { x = a[int(rand() * length(a)) + 1] if (!final[x]) { return x } } print "ERROR: could not find free pos for", name ". Try again!" exit(1) } END { while (rest) { min = 1000 for (i in npos) { if (min > npos[i]) { min = npos[i] nextper = i } } x = getrandpos(nextper, pers[nextper]) final[x] = nextper rest-- delete pers[nextper] delete npos[nextper] } for (i=1; i<=nseats; i++) { printf("box \"%s\"\n", final[i]); if (i == 8) { print "move right" } if (i == 12) { print "move right; up" } } print ".PE" }