! look for the end of the changing pattern. ! the pattern is just moving after some number of steps. ! So the final score is just increasing by the number of ! plants in the pattern. fill in the numbers below : 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 swap each >alist [ first2 CHAR: . = [ drop 0 ] when ] map sum ;