Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local input = io.read("*a")
- local tasks = {}
- local stack = {}
- local processes = {}
- local processors = {}
- local n
- local processing = 0
- for line in input:gmatch("[^\n]+") do
- n = tonumber(line)
- if not n then
- local arrow_location = line:find("->")
- local first_node = line:sub(1, arrow_location):match("%a+")
- local second_node = line:sub(arrow_location):match("%a+")
- tasks[first_node.."->"..second_node] = tonumber(line:match("%d+"))
- end
- end
- n = n or 1
- for i = 1, n do
- processors["P"..i] = false
- end
- for task in pairs(tasks) do
- if task:match("START") then
- processes[task] = true
- table.insert(stack, task)
- end
- end
- while processing < n do
- processing = 0
- for i = 1, n do
- local processor = "P"..i
- local last_task = processors[processor]
- if last_task then
- tasks[last_task] = tasks[last_task] - 1
- if tasks[last_task] > 0 then
- table.insert(stack, 1, last_task)
- else
- for task in pairs(tasks) do
- if not processes[task] then
- if last_task:reverse():match("%a+"):reverse() == task:match("%a+") then
- local can_add = true
- for other_task, time in pairs(tasks) do
- if other_task:reverse():match("%a+"):reverse() == task:match("%a+") and time > 0 then
- can_add = false
- end
- end
- if can_add then
- processes[task] = true
- table.insert(stack, task)
- end
- end
- end
- end
- end
- processors[processor] = false
- end
- if #stack > 0 then
- local task = table.remove(stack)
- local second_node = task:reverse():match("%a+"):reverse()
- io.write(processor, " processing task: ", second_node, "\n")
- processors[processor] = task
- else
- processing = processing + 1
- processors[processor] = false
- io.write(processor, ": no task available to process at this time.", "\n")
- end
- end
- end
- io.write("\n", "Processing complete!", "\n\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement