aboutsummaryrefslogtreecommitdiff
path: root/challenge-151/abigail/tcl/ch-2.tcl
blob: 591ac69870f597799003f1f194d460cedec261b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/local/opt/tcl-tk/bin/tclsh

#
# See https://theweeklychallenge.org/blog/perl-weekly-challenge-151
#

#
# Run as: tclsh ch-2.tcl < input-file
#

while {[gets stdin line] >= 0} {
    set h [split $line " "] 
    lappend h 0 0
    for {set i [expr [llength $h] - 3]} {$i >= 2} {incr i -1} {
        set v1 [expr [lindex $h $i] + [lindex $h [expr $i + 2]]]
        set v2 [lindex $h [expr $i + 1]]
        lset h $i [expr $v1 < $v2 ? $v2 : $v1]
    }
    puts [expr [lindex $h 0] + [lindex $h 2]]
}