-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
107 lines (77 loc) · 2.44 KB
/
solution.py
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
import sys
from Block import Block
DEBUG = False
def loadInput(inputPath):
lines: list[str] = []
blocks: list[Block] = []
with open(inputPath) as file:
lines = file.readlines()
productionCurve = list(map(int, lines[0].split(" ")))
lines.pop(0)
for i in range(len(lines)):
data = lines[i].split()
runtime = data[0]
cost = data[1]
deadline = data[2]
blocks.append(
Block(
i,
int(runtime),
int(cost),
int(deadline),
)
)
return productionCurve, blocks
def writeOutput(outputPath, tasksIds: list[list[Block]], prod):
lines: list[str] = []
with open(outputPath, "w") as f:
for tasks in tasksIds:
line = ""
line = " ".join(str(task.id) for task in tasks)
lines.append(line)
f.write("\n".join(lines))
def createLog(
id: int,
consumption: int,
production: int,
):
msg: list[int] = []
msg.append(id)
msg.append(consumption)
msg.append(production)
return " ".join(map(lambda msg: str(msg), msg))
def simpleSorting(production, blocks: list[Block]) -> list[list[Block]]:
allocatedBlocks: list[list[Block]] = []
consumption = [0 for _ in range(len(production))]
for i in range(len(production)):
selectedBlocks: list[Block] = []
for block in blocks:
if block.deadline <= i + block.runtime:
blocks.remove(block)
continue
if i + block.runtime > len(production):
blocks.remove(block)
continue
if block.cost + consumption[0] < production[i]:
selectedBlocks.append(block)
blocks.remove(block)
for j in range(selectedBlocks[-1].runtime):
consumption[j] += selectedBlocks[-1].cost
if DEBUG:
print(
createLog(
i,
consumption[0],
production[i],
)
)
allocatedBlocks.append(selectedBlocks)
consumption.pop(0)
return allocatedBlocks
if __name__ == "__main__":
inputPath = sys.argv[1]
outputPath = sys.argv[2]
production, tasks = loadInput(inputPath)
tasks.sort(key=lambda t: t.deadline)
tasks = simpleSorting(production, tasks)
writeOutput(outputPath, tasks, production)