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>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,n1,n2) for(int i=n1;i<n2;i++)
#define bFOR(i,n1,n2) for(int i=n1;i>=n2;i--)
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef long long int ll;
typedef pair<ll,ll> Pi;
const int INF=(ll)(1LL<<30)-1;
const double INFd=100000000000.0;
const ll INFl=(ll)9223372036854775807;
const int MAX=10000;
const ll MOD=(ll)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a, b)*b;}
int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0};
int mdx[4]={0,1,0,-1},mdy[4]={-1,0,1,0};


template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

string s;
int p;

int hash3(int t1){
	int a=(t1/1000);
	int b=(t1/100)%10;
	int c=(t1/10)%10;
	int d=t1%10;
	stack<int> stk;
	for(int i=s.size()-1;i>=0;i--){
		if(s[i]=='['||s[i]==']')continue;
		if(s[i]=='a')stk.push(a);
		if(s[i]=='b')stk.push(b);
		if(s[i]=='c')stk.push(c);
		if(s[i]=='d')stk.push(d);
		int x,y;
		if(s[i]=='+'||s[i]=='*'||s[i]=='^'){
			x=stk.top();stk.pop();y=stk.top();stk.pop();
		}
		if(s[i]=='+')stk.push(x|y);
		if(s[i]=='*')stk.push(x&y);
		if(s[i]=='^')stk.push(x^y);
	}
	return stk.top();
}	
int main(){
	while(1){
		cin>>s;
		if(s==".")break;
		cin>>p;
		int tt=hash3(p);
		cout<<tt<<" ";
		int ans=0;
		for(int i=0;i<10000;i++){
			if(tt==hash3(i))ans++;
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using LL = long long;

int id = 0;
string s;

int hhash(string p){
    int ret = -1;
    if(s[id] == '['){
        id++;
        switch(s[id++]){
        case '+':
            ret = hhash(p) | hhash(p);
            break;
        case '*':
            ret = hhash(p) & hhash(p);
            break;
        case '^':
            ret = hhash(p) ^ hhash(p);
            break;
        }
    }else{
        ret = p[s[id]-'a'] - '0';
    }
    id++;
    // cout << id << " " << ret << endl;
    return ret;
}

int main(){
    cin >> s;
    while(s != "."){
        string p;
        cin >> p;
        int ans = 0;
        id = 0;
        int enc = hhash(p);
        for(int i=0; i<10000; i++){
            string t = "";
            for(int j=1; j<10000; j*=10){
                t += (i/j)%10 + '0';
            }
            reverse(t.begin(), t.end());
            id = 0;
            if(hhash(t) == enc) ans++;
        }
        cout << enc << " " << ans << endl;
        cin >> s;
    }
}