#!/usr/local/bin/tclsh foreach row [split [read stdin] \n] { if {[scan $row {%[^)])%[^)]} a b] != 2} continue set orbits($b) $a } # First, record transfer costs from YOU to each node leading to COM. set sum 0 set o $orbits(YOU) while {$o ne "COM"} { set cost($o) $sum incr sum set o $orbits($o) } set cost(COM) $sum ;# just in case # Now, calculate cost from SAN until we intercept a marked node. set sum2 0 set o $orbits(SAN) while {$o ne "COM"} { if {[info exists cost($o)]} { puts [expr {$cost($o) + $sum2}] exit } incr sum2 set o $orbits($o) }