Paste: Project Euler 18

Author: Alyssa
Mode: factor
Date: Sun, 13 Nov 2011 22:45:46
Plain Text |
USING: kernel sequences splitting math.parser arrays math ;
IN: triangles

: sum-tri ( tri -- num ) [ sum ] map sum ;

: >-tri ( tri tri -- ? ) [ sum-tri ] bi@ > ;

: left-tri ( tri -- tri' ) rest [ but-last ] map ;

: right-tri ( tri -- tri' ) rest [ rest ] map ;

: >-right-tri ( tri -- ? ) [ right-tri ] [ left-tri ] bi >-tri ;

: bigger-tri ( tri -- tri' ) dup >-right-tri [ right-tri ] [ left-tri ] if ;

: grab ( tri -- num ) first first ;

: next-iter ( tri -- num tri' ) [ grab ] keep bigger-tri ;

: packed-next-iter ( {num,tri} -- {num',tri'} )
  [ first ] [ second ] bi next-iter [ + ] dip 2array ;

: max-tri ( {num,tri} -- num' ) 
  dup second length [ packed-next-iter ] times first ;

: read-tri ( string -- tri ) 
  string-lines [ " " split [ string>number ] map ] map ;

: solve* ( tri -- max ) 0 swap 2array max-tri ;

: solve ( string -- max-of-tri ) read-tri solve* ;

New Annotation

Summary:
Author:
Mode:
Body: