#!/usr/bin/env tclsh8.6 set scores {} while {[gets stdin line] >= 0} { set stack {} set corrupt 0 foreach c [split $line {}] { switch -- $c { \[ - \{ - ( - < {lappend stack $c} \] - \} - ) - > { set pop [lindex $stack end] set stack [lreplace $stack end end] } } switch -- $c { \] { if {$pop ne "\["} {set corrupt 1} } \} { if {$pop ne "\{"} {set corrupt 1} } ) { if {$pop ne "("} {set corrupt 1} } > { if {$pop ne "<"} {set corrupt 1} } } if {$corrupt} break } if {$corrupt} continue set score 0 while {[llength $stack]} { set pop [lindex $stack end] set stack [lreplace $stack end end] switch -- $pop { ( {set n 1} \[ {set n 2} \{ {set n 3} < {set n 4} } set score [expr {$score*5 + $n}] } lappend scores $score } set n [llength $scores] puts [lindex [lsort -integer $scores] [expr {$n/2}]]