-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha7.tt
85 lines (72 loc) · 2.5 KB
/
a7.tt
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
data step <'.'>
data dep <step>
composer step
{ dep: (<'Step '>) <'.'>, (<' must be finished before step '>) step: <'.'> (<' can begin.'>) }
end step
def deps: [ $IN::lines -> step ];
templates reduce&{operator:}
@: $(1);
$(2..last)... -> @reduce: [$@reduce, $] -> operator;
$@ !
end reduce
templates min
<?($(1)<..$(2)>)> $(1) !
<> $(2) !
end min
// part 1
templates topological
def waiting: [ $... -> $.step ];
{ unresolved: $,
ready: [ $... -> $.dep -> \(<?($waiting <~[<=$>]>)> $ !\) ] } -> #
when <{ready: <[](1..)>}> do
def current: $;
def next: $current.ready -> reduce&{operator:min};
$next !
def unresolved: [ $current.unresolved... -> \(<?($.dep <~=$next>)> $! \) ];
def waiting: [ $unresolved... -> $.step ];
{ unresolved: $unresolved,
ready: [ $current.ready... -> \(<~=$next> $ !\),
$current.unresolved... -> \(<?($.dep <=$next>)> $! \) -> $.step -> \(<?($waiting <~[<=$>]>)> $ !\)]
} -> #
end topological
$deps -> topological -> !OUT::write
'
' -> !OUT::write
// part 2
def tasks: ['ABCDEFGHIJKLMNOPQRSTUVWXYZ'... -> (step:$) -> $::value];
templates taskIndex
def value: $;
$tasks -> \[i](<=$value> $i !\)... !
end taskIndex
templates minByTime
<?($(1).time<..$(2).time>)> $(1) !
<> $(2) !
end minByTime
templates multi&{workers:, delay:}
@: 0;
def waiting: [ $... -> $.step ];
{ unresolved: $,
ready: [ $... -> $.dep -> \(<?($waiting <~[<=$>]>)> $ !\) ],
workers: []} -> #
$@ !
when <{workers: <[](..$workers-1)>, ready: <[](1..)>}> do
def current: $;
def next: $current.ready -> reduce&{operator:min};
{ workers: [ $current.workers..., {task: $next, time: $delay + ($next -> taskIndex) }],
ready: [$current.ready... -> \(<~=$next> $!\)],
unresolved: $current.unresolved } -> #
when <{workers: <[](1..)>}> do
def mintime: $.workers -> reduce&{operator:minByTime} -> $.time;
@: $mintime + $@;
def current: $;
def workers: [ $.workers... -> { task: $.task, time: $.time - $mintime } ];
def done: [$workers... -> \(<?($.time <=0"s">)> $ !\) -> $.task];
def unresolved: [ $current.unresolved... -> \(<?($done <~[<=$.dep>]>)> $! \) ];
def waiting: [ $unresolved... -> $.step ];
{ unresolved: $unresolved,
ready: [ $current.ready... -> \(<?($done <~[<=$>]>)> $ !\),
$current.unresolved... -> \(<?($done <[<=$.dep>]>)> $! \) -> $.step -> \(<?($waiting <~[<=$>]>)> $ !\)],
workers: [ $workers... -> \(<?($.time <1"s"..>)> $ !\)]
} -> #
end multi
$deps -> multi&{workers: 5, delay: 60"s"} -> !OUT::write