#!/usr/bin/env tclsh8.6 gets stdin line set crabs [lsort -integer [split $line ,]] proc triangle {n} { expr {$n * ($n+1) / 2} } set minfuel 99999999 for {set pos [lindex $crabs 0]} {$pos <= [lindex $crabs end]} {incr pos} { set fuel 0 foreach c $crabs { incr fuel [triangle [expr {abs($pos - $c)}]] } if {$fuel < $minfuel} {set minfuel $fuel} } puts $minfuel