Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	for {
		S := io.NextLine()
		if S == "." {
			break
		}
		P := io.NextLine()
		i := 0
		val := hash(S, P, &i)
		cnt := 0
		for j := 0; j < 10000; j++ {
			i = 0
			if hash(S, fmt.Sprintf("%04d", j), &i) == val {
				cnt++
			}
		}
		io.Println(val, cnt)
	}
}

func hash(s, n string, i *int) int {
	if s[*i] == '[' {
		*i++
		switch s[*i] {
		case '+':
			*i++
			l := hash(s, n, i)
			r := hash(s, n, i)
			*i++
			return l | r
		case '*':
			*i++
			l := hash(s, n, i)
			r := hash(s, n, i)
			*i++
			return l & r
		case '^':
			*i++
			l := hash(s, n, i)
			r := hash(s, n, i)
			*i++
			return l ^ r
		}
	}
	x := int(n[s[*i]-'a'] - '0')
	*i++
	return x
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧  
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i     
　　　　／　　　＼　 　  |　|     
　　　 /　　 /￣￣￣￣/　　|  
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿  
　 　　　＼/＿＿＿＿/　（u　⊃  
---------------------------------------------------------------------------------------------------*/





string S, P;
//---------------------------------------------------------------------------------------------------
int i, a, b, c, d;
int f() {
    if (S[i] == '[') {
        i++;
        int l, r, res;
        if (S[i] == '+') {
            i++;
            l = f();
            r = f();
            res = l | r;
        }
        else if (S[i] == '*') {
            i++;
            l = f();
            r = f();
            res = l & r;
        }
        else {
            i++;
            l = f();
            r = f();
            res = l ^ r;
        }
        i++;
        return res;
    }

    int res;
    if (S[i] == 'a') res = a;
    else if (S[i] == 'b') res = b;
    else if (S[i] == 'c') res = c;
    else res = d;

    i++;
    return res;
}
//---------------------------------------------------------------------------------------------------
void _main() {
    while (cin >> S) {
        if (S == ".") return;
        cin >> P;

        i = 0;
        a = P[0] - '0';
        b = P[1] - '0';
        c = P[2] - '0';
        d = P[3] - '0';
        int ha = f();
        int cnt = 0;
        rep(j, 0, 10000) {
            i = 0;
            a = j / 1000;
            b = (j / 100) % 10;
            c = (j / 10) % 10;
            d = j % 10;

            if (ha == f()) cnt++;
        }

        printf("%d %d\n", ha, cnt);
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < int(n); i++)

// T := c | [opTT]
int eval(string &s, string &pas, int &i){
    if(isalpha(s[i])){
        int pos = s[i]-'a';
        i++;
        return pas[pos]-'0';
    }
    assert(s[i] == '[');
    i++; // [
    char op = s[i]; i++;
    int x = eval(s,pas,i);
    int y = eval(s,pas,i);
    i++; // ]
    if(op == '+') return x | y;
    if(op == '*') return x & y;
    if(op == '^') return x ^ y;
    assert(false);
}

int main(void){
    string s;
    while(cin>>s, s != "."){
        string p;
        cin>>p;
        int index = 0;
        int hash = eval(s,p,index);
        int cnt = 0;
        rep(i,10000){
            index = 0;
            string str = to_string(i);
            while((int)str.size() < 4){
                str = "0"+str;
            }
            if(eval(s,str,index) == hash) cnt++;
        }
        cout<<hash<<" "<<cnt<<endl;
    }

}