1 ! Copyright (c) 2007 Samuel Tardieu, Aaron Schaefer.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: io.files math.parser project-euler.common
4 io.encodings.ascii sequences splitting ;
7 ! https://projecteuler.net/problem=67
12 ! By starting at the top of the triangle below and moving to
13 ! adjacent numbers on the row below, the maximum total from top
21 ! That is, 3 + 7 + 4 + 9 = 23.
23 ! Find the maximum total from top to bottom in triangle.txt
24 ! (right click and 'Save Link/Target As...'), a 15K text file
25 ! containing a triangle with one-hundred rows.
27 ! NOTE: This is a much more difficult version of Problem 18. It
28 ! is not possible to try every route to solve this problem, as
29 ! there are 2^99 altogether! If you could check one trillion
30 ! (10^12) routes every second it would take over twenty billion
31 ! years to check them all. There is an efficient algorithm to
38 ! Propagate from bottom to top the longest cumulative path as is
43 : source-067 ( -- seq )
44 "resource:extra/project-euler/067/triangle.txt"
45 ascii file-lines [ split-words [ string>number ] map ] map ;
49 : euler067 ( -- answer )
50 source-067 propagate-all first first ;
52 ! [ euler067 ] 100 ave-time
53 ! 20 ms ave run time - 2.12 SD (100 trials)
59 : euler067a ( -- answer )
62 ! [ euler067a ] 100 ave-time
63 ! 21 ms ave run time - 2.65 SD (100 trials)