Posted to tcl by schelte at Sat Dec 03 23:16:04 GMT 2022view raw
- #!/usr/bin/tclsh
- proc prio {ch} {
- set p [scan $ch "%c"]
- return [expr {$p > 96 ? $p - 96 : $p - 64 + 26}]
- }
- set input [read stdin]
- set t0 0
- set t1 0
- foreach line [split $input "\n"] {
- set items [split $line {}]
- set n [expr {[llength $items] / 2}]
- foreach ch [lrange $items 0 [expr {$n - 1}]] {
- if {[lsearch -exact -start $n $items $ch] >= 0} {
- incr t0 [prio $ch]
- break
- }
- }
- foreach ch [lsort -unique $items] {
- dict incr count $ch
- }
- if {([incr linenum] % 3) == 0} {
- foreach ch [dict keys [dict filter $count value 3]] {
- incr t1 [prio $ch]
- }
- set count {}
- }
- }
- puts "part1: $t0"
- puts "part2: $t1"