#!/usr/local/bin/tclsh foreach row [split [read stdin] \n] { if {[scan $row {%[^)])%[^)]} a b] != 2} continue set orbits($b) $a set object($b) 1 set object($a) 1 } set sum 0 foreach o [array names object] { while {$o ne "COM"} { incr sum set o $orbits($o) } } puts $sum