#!/usr/bin/env tclsh8.6 while {[gets stdin line] >= 0} { if {$line eq ""} continue lassign [split $line -] a b lappend paths($a) $b lappend paths($b) $a set big($a) [string match {*[A-Z]*} $a] set big($b) [string match {*[A-Z]*} $b] } proc explore {node beento} { global paths big count foreach n $beento {set been($n) 1} lappend beento $node foreach n $paths($node) { if {$n eq "end"} {incr count; continue} if {$big($n) || ! [info exists been($n)]} { explore $n $beento } } } set count 0 explore start {} puts $count