: befores ( seq -- hash ) H{ } clone [ [ [ first2 ] dip push-at ] curry each ] keep ; : afters ( seq -- graph ) H{ } clone [ [ [ first2 swap ] dip push-at ] curry each ] keep ; : roots ( seq befores -- roots ) [ concat members ] [ keys ] bi* diff ; : cost-p1 ( str -- n ) drop 0 ; : cost-p2 ( str -- n ) first CHAR: A - 61 + ; : init-heap ( seq befores -- heap ) roots [ t ] H{ } map>assoc [ heap-push-all ] keep ; :: schedule ( cost-quot: ( str -- cost ) workers -- order time ) "/tmp/input" ascii file-lines [ " " split { 1 7 } swap nths ] map :> seq seq befores :> b seq afters :> a seq b init-heap :> h :> working 0 :> curtime! seq concat members fast-set :> remaining :> r [ remaining null? ] [ [ working heap-size workers < h heap-size 0 > and ] [ h heap-pop nip :> cur cur cur cost-quot call curtime + working heap-push ] while working heap-pop curtime! :> cur t cur r set-at cur remaining delete cur a at [| after | after b at [ r at ] all? [ t after h heap-push ] when ] each ] until r keys concat curtime ; inline "p1:" print [ cost-p1 ] 1 schedule . . nl "p2:" print [ cost-p2 ] 5 schedule . .