Paste: aoc12 part1
Author: | jonenst |
Mode: | factor |
Date: | Wed, 12 Dec 2018 11:34:59 |
Plain Text |
: trim-ends ( state -- state' )
[
[ dup keys natural-sort [ swap at CHAR: # = ] with find nip 2 - ]
[ dup keys natural-sort [ swap at CHAR: # = ] with find-last nip 2 + ] bi [a,b]
] [ [ dupd at CHAR: . or ] curry H{ } map>assoc ] bi ;
: extend-keys ( keys -- keys' )
dup [ first [ 2 - ] keep [a,b) ] [ last [ 2 + ] keep (a,b] ] bi surround ;
: (grow) ( idx state rules -- plant )
[ [ [ 2 - ] [ 2 + ] bi [a,b] ] dip [ at CHAR: . or ] curry map >string ] [ at CHAR: . or ] bi* ;
: grow ( state rules -- state' )
over keys natural-sort extend-keys [ [ -rot (grow) ] keep swap ] 2with H{ } map>assoc trim-ends ;
: aoc12-1 ( -- n )
"/tmp/input" ascii file-lines unclip ": " split last [ 2array ] map-index [ first2 swap ] H{ } map>assoc
swap rest [ " => " split [ first ] [ last first ] bi ] H{ } map>assoc
[
grow
] curry 20 swap times >alist [ first2 CHAR: . = [ drop 0 ] when ] map sum
;
Author: | jonenst |
Mode: | factor |
Date: | Wed, 12 Dec 2018 15:33:42 |
Plain Text |
: p2 ( n -- m )
1 - 107 - 52 * 6535 + ;
: trim-ends ( state -- state' )
[
[ dup keys natural-sort [ swap at CHAR: # = ] with find nip 2 - ]
[ dup keys natural-sort [ swap at CHAR: # = ] with find-last nip 2 + ] bi [a,b]
] [ [ dupd at CHAR: . or ] curry H{ } map>assoc ] bi ;
: extend-keys ( keys -- keys' )
dup [ first [ 2 - ] keep [a,b) ] [ last [ 2 + ] keep (a,b] ] bi surround ;
: (grow) ( idx state rules -- plant )
[ [ [ 2 - ] [ 2 + ] bi [a,b] ] dip [ at CHAR: . or ] curry map >string ] [ at CHAR: . or ] bi* ;
: grow ( state rules -- state' )
over keys natural-sort extend-keys [ [ -rot (grow) ] keep swap ] 2with H{ } map>assoc trim-ends ;
: aoc12-1 ( -- n )
"/tmp/input" ascii file-lines unclip ": " split last [ 2array ] map-index [ first2 swap ] H{ } map>assoc
swap rest [ " => " split [ first ] [ last first ] bi ] H{ } map>assoc
[ [ pprint ] dip
grow dup >alist natural-sort values >string write dup >alist [ first2 CHAR: . = [ drop 0 ] when ] map sum .
] curry 200 <iota> swap each >alist [ first2 CHAR: . = [ drop 0 ] when ] map sum
;
New Annotation