#!/usr/bin/env tclsh9.0 namespace import ::tcl::mathop::+ proc input {} { global nodes edges set nodes [dict create] ;# existence set edges [dict create] ;# maps each node to a list of neighbors while {[gets stdin line] >= 0} { if {$line eq ""} continue if {[scan $line {%[^-]-%s} a b] != 2} continue dict set nodes $a 1 dict set nodes $b 1 dict lappend edges $a $b dict lappend edges $b $a } } proc setsof3 {} { global nodes edges set sets [dict create] ;# existence foreach node [dict keys $nodes] { set neighbors [dict get $edges $node] if {[llength $neighbors] < 2} continue # We have at least 2 neighbors, so consider each pair. for {set i 0} {$i < [llength $neighbors] - 1} {incr i} { for {set j [+ $i 1]} {$j < [llength $neighbors]} {incr j} { set a [lindex $neighbors $i] set b [lindex $neighbors $j] if {[string range $node 0 0] ne "t" && [string range $a 0 0] ne "t" && [string range $b 0 0] ne "t"} continue if {[connected $node $a $b]} { set tmp [lsort [list $node $a $b]] dict set sets [join $tmp ,] 1 } } } } # dict for {key val} $sets { # puts $key # } puts [dict size $sets] } proc connected {a b c} { global edges set aneigh [dict get $edges $a] set bneigh [dict get $edges $b] if {$c ni $aneigh || $c ni $bneigh || $a ni $bneigh} {return 0} return 1 } input setsof3