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<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<bitset>
#include<stack>
#include<memory>
using namespace std;
using namespace std::chrono;
typedef long long int llint;
typedef double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/
const llint mod=998244353;
const llint big=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-15;
template <class T,class U>bool mineq(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool maxeq(T& a,U b){if(a<b){a=b;return true;}return false;}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}
int pass[4];
int bas;
string S;
int rrr(void){
	if(S[bas]=='a'){bas++;return pass[0];}
	if(S[bas]=='b'){bas++;return pass[1];}
	if(S[bas]=='c'){bas++;return pass[2];}
	if(S[bas]=='d'){bas++;return pass[3];}
	
		bas++;char op=S[bas];bas++;
		int x=rrr();
		int y=rrr();
		bas++;
		if(op=='+'){return x|y;}
		if(op=='*'){return x&y;}
		if(op=='^'){return x^y;}
	
}
bool  solve(void){
	cin>>S;
	if(S=="."){return false;}
	int aaa;cin>>aaa;
	bas=0;
	pass[3]=aaa%10;aaa/=10;
	pass[2]=aaa%10;aaa/=10;
	pass[1]=aaa%10;aaa/=10;
	pass[0]=aaa%10;aaa/=10;
	int myh=rrr();
	int ans=0;
	for(int i=0;i<10000;i++){
		aaa=i;
		pass[3]=aaa%10;aaa/=10;
		pass[2]=aaa%10;aaa/=10;
		pass[1]=aaa%10;aaa/=10;
		pass[0]=aaa%10;aaa/=10;
		bas=0;
		if(rrr()==myh){ans++;}
	}
	cout<<myh<<" "<<ans<<endl;
	return true;
}
int main(void){
	while(solve()){}
	
	return 0;
}

C++: #include <bits/stdc++.h>
#include <cctype>
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define all(x) (x).begin(),(x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(),(V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
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; }
const ll INF = 1e15;
typedef pair<ll,ll> P;
typedef string::const_iterator State;
class ParseError {};
int expression(State &begin);
int term(State &begin);
//int number(State &begin);
//int factor(State &begin);
string s;
int expression(State &begin){
    int ret;
    for(;;){
        if (*begin == '['){
            begin++;
            ret = term(begin);
        }
        else if (*begin == ']'){
            begin++;
            break;
        }
        else if (*begin == 'a'||*begin == 'b'||*begin == 'c'||*begin == 'd'){
            ret = s[*begin - 'a'] - '0';
            begin++;
            break;
        }
        else{
            break;
        }
    }
    return ret;
}
string fill(int s){
    string t = to_string(s);
    int a = 4-t.length();
    rep(i,a){
        t = '0'+t;
    }
    return t;
}
int term(State &begin){
    int ret;
    for(;;){
        if (*begin == '+'){
            begin++;
            ret = expression(begin) | expression(begin);
        }
        else if (*begin == '*'){
            begin++;
            ret = expression(begin) & expression(begin);
        }
        else if (*begin == '^'){
            begin++;
            ret = expression(begin) ^ expression(begin);
        }
        else break;
    }
    return ret;
}

int main(){
    string ex;
    int a;
    for(;;){
        int c = 0;
        getline(cin,ex);
        if (ex == ".") break;
        cin >> s;
        cin.ignore();
        State begin = ex.begin();
        a = expression(begin);
        rep(i,10000){
            State begin = ex.begin();
            s = fill(i);
            if (a == expression(begin)) c++;
        }
        cout << a << ' ' << c << '\n';
    }
}

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

int pos;
int a,b,c,d;
string s;

int Hash(void){
	int ret;
	if(s[pos]=='['){
		++pos;
		int L,R;
		if(s[pos]=='+'){
			++pos;
			L=Hash();	R=Hash();
			ret=L|R;
		}else if(s[pos]=='*'){
			++pos;
			L=Hash();	R=Hash();
			ret=L&R;
		}else if(s[pos]=='^'){
			++pos;
			L=Hash();	R=Hash();
			ret=L^R;
		}

		++pos;
		return ret;
	}

	if(s[pos]=='a')ret=a;
	if(s[pos]=='b')ret=b;
	if(s[pos]=='c')ret=c;
	if(s[pos]=='d')ret=d;

	++pos;
	return ret;
}

int main(){
	while(1){
		cin>>s;		if(s==".")break;
		string p;	cin>>p;
		a=p[0]-'0';
		b=p[1]-'0';
		c=p[2]-'0';
		d=p[3]-'0';

		pos=0;
		int H=Hash();

		int ans=0;
		for(int i=0; i<10000; i++){
			a=i/1000;
			b=(i%1000)/100;
			c=(i%100)/10;
			d=i%10;

			pos=0;
			if(H==Hash())ans++;
		}

		printf("%d %d\n",H,ans);
	}

	return 0;
}