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 <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>

using namespace std;
#define int long long int
const int INF = 1001001001001001LL;

int di[] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dj[] = {0, 1, 0, -1, 1, 1, -1, -1};


int f(const string& s, char a, char b, char c, char d, int l, int r){
    //cout << l << " " << r << " " << s[l] << " " << s[r] << endl;
    if(r == l){
        
        
        if(s[l] == 'a') return a - '0';
        if(s[l] == 'b') return b - '0';
        if(s[l] == 'c') return c - '0';
        if(s[l] == 'd') return d - '0';
    }
    
    assert(s[l] == '[');
    assert(s[r] == ']');
    char op = s[l + 1];
    //cerr << op << endl;
    int x, y;
    l += 2;
    if(s[l] != '['){
        x = f(s, a, b, c, d, l, l);
        l++;
    }else{
        int nr = l;
        while(s[nr] != ']'){
            nr++;
        }
        x = f(s, a, b, c, d, l, nr);
        l = nr + 1;
    }

    
    if(s[l] != '['){
        y = f(s, a, b, c, d, l, l);
        l++;
    }else{
        int nr = l;
        while(s[nr] != ']'){
            nr++;
        }
        y = f(s, a, b, c, d, l, nr);
        l = nr + 1;
    }

    int ret = 0;
    if(op == '+') ret = x | y;
    else if(op == '*') ret = x & y;
    else ret = x ^ y;
    

    //cerr << op << " " << x << " " << y << " " << ret << endl;
    return ret;

}

signed main(){
    
    //cin >> s;
    //cout << f('0', '4', '0', '4', 0, s.size()) << endl;
    
    while(1){
        string s; cin >> s;
        if(s == ".") break;
        string p; cin >> p;
        int n = s.size();

        int val = f(s, p[0], p[1], p[2], p[3], 0, n - 1);
        int ans = 0;
        
        for(char a = '0'; a <= '9'; a++){
            for(char b = '0'; b <= '9'; b++){
                for(char c = '0'; c <= '9'; c++){
                    for(char d = '0'; d <= '9'; d++){
                        if(val == f(s, a, b, c, d, 0, n - 1)) ans++;
                    }
                }
            }
        }

        cout << val << " " << ans << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
string s;
int a,b,c,d;
int eval(int n)
{
	d=n%10;n/=10;c=n%10;n/=10;b=n%10;n/=10;a=n;
	stack<int>S;
	int A,B;
	for(int i=0;i<s.size();i++)
	{
		switch(s[i])
		{
			case'a':S.push(a);break;
			case'b':S.push(b);break;
			case'c':S.push(c);break;
			case'd':S.push(d);break;
			case'*':
				A=S.top();S.pop();
				B=S.top();S.pop();
				S.push(A&B);
				break;
			case'+':
				A=S.top();S.pop();
				B=S.top();S.pop();
				S.push(A|B);
				break;
			case'^':
				A=S.top();S.pop();
				B=S.top();S.pop();
				S.push(A^B);
				break;
		}
	}
	return S.top();
}
main()
{
	while(cin>>s,s!=".")
	{
		reverse(s.begin(),s.end());
		int p;cin>>p;
		int cnt[20]={};
		for(int i=0;i<1e4;i++)cnt[eval(i)]++;
		cout<<eval(p)<<" "<<cnt[eval(p)]<<endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = (A); I < (B); ++I)
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //ai>=v  x is sorted
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //ai>v  x is sorted
#define NUM(x,v) (POSU(x,v)-POSL(x,v))  //x is sorted
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define REV(x) (reverse(x.begin(),x.end())) //reverse
#define TO(x,t,f) ((x)?(t):(f))
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define FILV(x,a) fill(x.begin(),x.end(),a)
#define FILA(ar,N,a) fill(ar,ar+N,a)
#define NEXTP(x) next_permutation(x.begin(),x.end())
ll gcd(ll a,ll b){if(a<b)swap(a,b);if(a%b==0)return b;else return gcd(b,a%b);}
ll lcm(ll a,ll b){ll c=gcd(a,b);return ((a/c)*(b/c)*c);}//saisyo kobaisu
#define pb push_back
#define pri(aa) cout<<(aa)<<endl
#define mp(x,y) make_pair(x,y)
#define fi first
#define se second
const ll INF=1e18+7;
const ll MOD=1e9+7;
const ll N = 1007;

ll pnum[4];
ll chartonum(char x){
	if(x=='+')return -1;
	if(x=='*')return -2;
	if(x=='^')return -3;
	else return pnum[x-'a'];
}

ll calc3(ll op,ll a,ll b){
	if(op==-1)return (a|b);//+
	if(op==-2)return (a&b);//*
	if(op==-3)return (a^b);//^
}

ll anscalc(string s){
	stack<ll> op;
	FOR(i,0,s.size()){
		if(s[i]=='[' || s[i]==']')continue;
		else op.push(chartonum(s[i]));
		while(op.size()>2){
			ll top1,top2,top3;
			top1 = op.top();op.pop();
			top2 = op.top();op.pop();
			top3 = op.top();op.pop();
			if(top3<0 && top2>=0 && top1>=0){
				op.push(calc3(top3,top1,top2));
			}else{
				op.push(top3);
				op.push(top2);
				op.push(top1);
				break;
			}
		}
	}
	return op.top();
}


int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	while(1){
		ll ans,num=0;
		string s,p;
		cin>>s;
		if(s[0]=='.')break;
		cin>>p;
		FOR(i,0,4)pnum[i] = p[i]-'0';
		ans = anscalc(s);
		FOR(i,0,10){
			FOR(j,0,10){
				FOR(k,0,10){
					FOR(l,0,10){
						pnum[0]=i;
						pnum[1]=j;
						pnum[2]=k;
						pnum[3]=l;
						if(anscalc(s)==ans)num++;
					}
				}
			}
		}
		cout<<ans<<" "<<num<<endl;
	}

}