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 <string>
#include <map>
#include <vector>
#include <utility>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <numeric>

#define rep(i,j,k) for(int i=(int)j;i<(int)k;i++)
#define itrep(i,x) for(auto i=(x).begin(); i!=(x).end();i++)
#define Sort(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define vec vector
#define INF (int)1e9
#define INFL 1e18
#define MOD 1000000007
#define pb push_back
#define MP make_pair
#define PI 3.1415926535
#define fcout cout<<fixed<<setprecision(10)
typedef long long int ll;
typedef std::pair<int,int> P;
int D=1;
int dx[4]={0,1,0,-1},dy[4]={-1,0,1,0};
int day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
//template<typename T>void pp(T x){cout<<x<<endl;}

using namespace std;
template<typename T> void test(vector<T> v){for(int i=0;i<v.size();i++){if(i)cout<<" ";cout<<v[i];}cout<<endl;}

int andd(int x,int y){
    int ret=0;
    rep(i,0,4){
        ret+=pow(2,i)*((x%2)&&(y%2));
        x/=2; y/=2;
    }
    return ret;
}

int orr(int x,int y){
    int ret=0;
    rep(i,0,4){
        ret+=pow(2,i)*((x%2)||(y%2));
        x/=2; y/=2;
    }
    return ret;

}

int hashh(string s,string ss){
    stack<int> st;
    int a,b,c,d;
    d=ss[3]-'0'; c=ss[2]-'0'; b=ss[1]-'0'; a=ss[0]-'0';
    reverse(all(s));
    rep(i,0,s.size()){
        if(s[i]=='a')st.push(a);
        else if(s[i]=='b')st.push(b);
        else if(s[i]=='c')st.push(c);
        else if(s[i]=='d')st.push(d);
        else if(s[i]=='+'){
            int x=st.top();
            st.pop();
            int y=st.top();
            st.pop();
            st.push(orr(x,y));
        }else if(s[i]=='*'){
            int x=st.top();
            st.pop();
            int y=st.top();
            st.pop();
            st.push(andd(x,y));
        }else if(s[i]=='^'){
            int x=st.top();
            st.pop();
            int y=st.top();
            st.pop();
            st.push(x^y);
        }
    }
    return st.top();
}
int main(){
    string s;
    while(cin>>s&&s!="."){
        string ss;
        cin>>ss;
        int ans=hashh(s,ss);
        cout<<ans<<endl;
        ss="0000";
        int ct=0;
        rep(i,0,10)rep(j,0,10)rep(k,0,10)rep(l,0,10){
            ss[0]='0'+i;
            ss[1]='0'+j;
            ss[2]='0'+k;
            ss[3]='0'+l;
            if(ans==hashh(s,ss)){
                ct++;
            }
        }
        cout<<ct<<endl;
    }
    return 0;
}

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

string s;
int p,a,b,c,d,now=0,pp;

int solve();


int main(){
  int i,j,k,l,ansc;
  while(1){
    cin >> s;
    if(s==".")break;
    ansc=0;
    cin >> p;
    d=p%10;
    p/=10;
    c=p%10;
    p/=10;
    b=p%10;
    p/=10;
    a=p%10;
    now=0;
    pp = solve();
    for(i=0;i<10;i++)for(j=0;j<10;j++)for(k=0;k<10;k++)for(l=0;l<10;l++){
	    a = i;
	    b = j;
	    c = k;
	    d = l;
	    now = 0;
	    if(pp==solve())ansc++;
	  }
    cout << pp << ' '<< ansc << endl;
  }
  return 0;
}

int solve(){
  int ans1=0,ans2=0;
  char x;
  if(s[now]=='['){
    now++;
    x=s[now];
    now++;
    ans1 = solve();
    ans2 = solve();
    now++;
    //cout << ans1<< " " << ans2 <<" "<< x<< endl;
    if(x=='+')return ans1|ans2;
    else if(x=='*')return ans1&ans2;
    else if(x=='^')return ans1^ans2;
  }
  if(s[now]>='a'||s[now]<='d'){
    now++;
    //cout << s[now-1] << endl;
    if(s[now-1]=='a')return a;
    if(s[now-1]=='b')return b;
    if(s[now-1]=='c')return c;
    if(s[now-1]=='d')return d;
  }
}

C++: #include<bits/stdc++.h>

#define INF 1e9
#define llINF 1e18
#define MOD 1e9+7
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
using namespace std;
string hash;
int a,b,c,d;
int po=0;
int op(int left,int right,char ope){
  if(ope=='+')
    return left | right;
  if(ope=='^')
    return left ^ right;
  if(ope=='*')
    return left & right;
}

int dfs(){
  if(hash[po]=='['){
    po++;
    dfs();
  }else if(hash[po]=='+'||hash[po]=='*'||hash[po]=='^'){
    char ope=hash[po];
    po++;
    int left=dfs();
    int right=dfs();
    po++;
    return op(left,right,ope);
  }else{
    if(hash[po]=='a'){
      po++;
      return a;
    }else if(hash[po]=='b'){
      po++;
      return b;
    }else if(hash[po]=='c'){
      po++;
      return c;
    }else if(hash[po]=='d'){
      po++;
      return d;
    }
  }
}
int main(){
  while(cin>>hash,hash!="."){
    po=0;
    string num;cin>>num;
    a=num[0]-'0';b=num[1]-'0';
    c=num[2]-'0';d=num[3]-'0';
    int ori=dfs();
    int cnt=0;
    for(int i=0;i<10000;i++){
      po=0;
      a=i/1000;b=(i/100)%10;
      c=(i/10)%10;d=i%10;
      if(ori==dfs())cnt++;
    }
    cout<<ori<<" "<<cnt<<endl;
  }
  return 0;
}