-
Notifications
You must be signed in to change notification settings - Fork 1
/
nexter.py
executable file
·78 lines (64 loc) · 2.33 KB
/
nexter.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
"""Nexter functions.
Author
--------
Adrien Pouyet
"""
def naive(first_tasks, remaining_tasks, tasks_d):
"""Naive nexter. Yields next element in remaining_tasks."""
for t in remaining_tasks:
yield t
class FillGaps:
"""Class to store static data."""
gaps_ab_arr = None
gaps_bc_arr = None
@staticmethod
def init():
"""Set gaps_ab_arr and gaps_bc_arr to None."""
FillGaps.gaps_ab_arr = None
FillGaps.gaps_bc_arr = None
@staticmethod
def fill_gaps_a_b(first_tasks, remaining_tasks, tasks_d):
"""Try to fill gaps between machine A and machine B."""
if FillGaps.gaps_ab_arr is None:
FillGaps.gaps_ab_arr = sorted(first_tasks + remaining_tasks, key=lambda a: tasks_d[a][0] - tasks_d[a][1])
i = 0
left = 0
right = -1
last_was_b = True # Last was long b short a
while i < len(remaining_tasks):
if last_was_b:
while FillGaps.gaps_ab_arr[right] not in remaining_tasks:
right -= 1
yield FillGaps.gaps_ab_arr[right]
right -= 1
else:
while FillGaps.gaps_ab_arr[left] not in remaining_tasks:
left += 1
yield FillGaps.gaps_ab_arr[left]
left += 1
last_was_b = ~last_was_b
i += 1
@staticmethod
def fill_gaps_b_c(first_tasks, remaining_tasks, tasks_d):
"""Try to fill gaps between machine C and machine B."""
if FillGaps.gaps_bc_arr is None:
FillGaps.gaps_bc_arr = sorted(first_tasks + remaining_tasks, key=lambda a: tasks_d[a][1] - tasks_d[a][2])
i = 0
left = 0
right = -1
last_was_c = True # Last was long c short b
while i < len(remaining_tasks):
if last_was_c:
while FillGaps.gaps_bc_arr[right] not in remaining_tasks:
right -= 1
yield FillGaps.gaps_bc_arr[right]
right -= 1
else:
while FillGaps.gaps_ab_arr[left] not in remaining_tasks:
left += 1
yield FillGaps.gaps_bc_arr[left]
left += 1
last_was_c = ~last_was_c
i += 1
fill_gaps_a_b = FillGaps.fill_gaps_a_b
fill_gaps_b_c = FillGaps.fill_gaps_b_c