Paste: aoc07
Author: | jon |
Mode: | factor |
Date: | Fri, 8 Dec 2017 15:52:47 |
Plain Text |
USING: accessors assocs grouping io.encodings.utf8 io.files
kernel math math.parser prettyprint sequences splitting ;
IN: aoc.2017.07
TUPLE: node name weight children total ;
C: <node> node
: parse-line ( line -- value key )
" ,->()" split harvest dup 1 swap [ string>number ] change-nth
2 cut [ first2 ] dip f <node>
dup name>> ;
: parseit ( lines -- map )
H{ } clone [ [
[ parse-line ] [ set-at ] bi*
] curry each ] keep ;
: find-rootname ( assoc -- root )
[ keys ] [ values [ children>> ] map concat ] bi
[ member? not ] curry find nip ;
: inline-graph ( node assoc -- )
[ [ [ at ] [ dupd inline-graph ] bi ] curry map ] curry change-children drop ;
: compute-totals ( node -- total )
[ children>> [ compute-totals ] map-sum ]
[ weight>> + ] [ dupd total<< ] tri ;
: aoc-07-2 ( -- n )
"/tmp/input" utf8 file-lines
parseit dup find-rootname dup .
over at
[ swap inline-graph ]
[ compute-totals drop ]
[ find-imbalance ] tri ;
New Annotation