: 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 ; : init-heap ( seq befores -- heap ) roots [ t ] H{ } map>assoc [ heap-push-all ] keep ; :: aoc7-1 ( -- ) "/tmp/input" ascii file-lines [ " " split { 1 7 } swap nths ] map :> seq seq befores :> b seq afters :> a seq b init-heap :> h :> r [ h heap-empty? ] [ h heap-pop nip :> cur t cur r set-at cur a at [| after | after b at [ r at ] all? [ t after h heap-push ] when ] each ] until r keys concat print ; aoc7-1