How to resolve the algorithm Maximum triangle path sum step by step in the Factor programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Maximum triangle path sum step by step in the Factor programming language

Table of Contents

Problem Statement

Starting from the top of a pyramid of numbers like this, you can walk down going one step on the right or on the left, until you reach the bottom row: One of such walks is 55 - 94 - 30 - 26. You can compute the total of the numbers you have seen in such walk, in this case it's 205. Your problem is to find the maximum total among all possible paths from the top to the bottom row of the triangle. In the little example above it's 321.

Find the maximum total in the triangle below: Such numbers can be included in the solution code, or read from a "triangle.txt" file. This task is derived from the Euler Problem #18.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Maximum triangle path sum step by step in the Factor programming language

Source code in the factor programming language

USING: grouping.extras io.encodings.utf8 io.files kernel
math.order math.parser math.vectors prettyprint sequences
splitting ;
IN: rosetta-code.maximum-triangle-path-sum

: parse-triangle ( path -- seq )
    utf8 file-lines [ " " split harvest ] map
    [ [ string>number ] map ] map ;

: max-triangle-path-sum ( seq -- n )
    <reversed> unclip-slice [ swap [ max ] 2clump-map v+ ]
    reduce first ;

"triangle.txt" parse-triangle max-triangle-path-sum .


  

You may also check:How to resolve the algorithm Loops/With multiple ranges step by step in the RPL programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Wren programming language
You may also check:How to resolve the algorithm Soundex step by step in the Phix programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the BQN programming language
You may also check:How to resolve the algorithm Strip a set of characters from a string step by step in the ScriptBasic programming language