#!/usr/bin/env tclsh9.0

proc input {} {
    global A B C prog ip
    while {[gets stdin line] >= 0} {
        if {$line eq ""} continue
        if {[string match "Register A:*" $line]} {
            set A [lindex [split $line " "] end]
        } elseif {[string match "Register B:*" $line]} {
            set B [lindex [split $line " "] end]
        } elseif {[string match "Register C:*" $line]} {
            set C [lindex [split $line " "] end]
        } elseif {[string match "Program:*" $line]} {
            set prog [split [lindex [split $line " "] 1] ,]
        }
    }
    set ip 0
}

proc show {} {
    global A B C prog
    puts "A=$A B=$B C=$C"
    puts "prog=$prog"
}

proc combo {op} {
    global A B C ip
    switch -- $op {
        0 - 1 - 2 - 3 {return $op}
        4 {return $A}
        5 {return $B}
        6 {return $C}
        7 {puts stderr "combo operator 7 used, ip=$ip"; exit 1}
    }
}

proc run {} {
    global A B C prog ip
    set output [list]
    while {$ip >= 0 && $ip < [llength $prog]} {
        set op [lindex $prog $ip+1]
        switch -- [lindex $prog $ip] {
            0 {set A [expr {$A / 2**[combo $op]}]}
            1 {set B [expr {$B ^ $op}]}
            2 {set B [expr {[combo $op] & 7}]}
            3 {if {$A != 0} {set ip $op; continue}}
            4 {set B [expr {$B ^ $C}]}
            5 {lappend output [expr {[combo $op] & 7}]}
            6 {set B [expr {$A / 2**[combo $op]}]}
            7 {set C [expr {$A / 2**[combo $op]}]}
        }
        incr ip 2
    }
    puts [join $output ,]
}

input
# show
run