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