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;
typedef string::const_iterator State;
class ParseError{};

int letter(State& begin,const int& a,const int& b,const int& c,const int& d){
    char let = *begin;
    begin++;
    switch(let){
        case 'a':
            return a;
        case 'b':
            return b;
        case 'c':
            return c;
        case 'd':
            return d;
        default:
            throw ParseError();
    }
}

int Hash(State& begin,const int& a,const int& b,const int& c,const int& d){
    if(*begin == '['){
        begin++;
        char op = *begin;
        begin++;
        int hash1 = Hash(begin,a,b,c,d);
        int hash2 = Hash(begin,a,b,c,d);
        begin++;
        switch(op){
            case '+':
                return hash1 | hash2;
            case '*':
                return hash1 & hash2;
            case '^':
                return hash1 ^ hash2;
            default:
                throw ParseError();
        }
    }
    return letter(begin,a,b,c,d);
}

void solve(string S,string P){
    State begin = S.begin();
    int hash = Hash(begin,P[0]-'0',P[1]-'0',P[2]-'0',P[3]-'0');
    int res = 0;
    vector<int> v;
    for(int i = 0;i < 10;i++)v.push_back(i);
    for(auto a:v)for(auto b:v)for(auto c:v)for(auto d:v){
        begin = S.begin();
        if(hash == Hash(begin,a,b,c,d))res++;
    }
    cout << hash << ' ' << res << endl;
}

int main(){
    string S,P;
    while(true){
        cin >> S;
        if(S == ".")return 0;
        cin >> P;
        solve(S,P);
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;

string s,t;
int k;
ll dfs() {
  char c=s[k++];
  if(c=='[') {
    ll x=dfs();
    k++;
    return x;
  } else if(isalpha(c)) return t[c-'a']-'0';
  ll x=0,y=0;
  if(s[k]=='[') {
    k++;
    x=dfs();
    k++;
  } else x=t[s[k++]-'a']-'0';
  if(s[k]=='[') {
    k++;
    y=dfs();
    k++;
  } else y=t[s[k++]-'a']-'0';
  if(c=='*') x&=y;
  else if(c=='+') x|=y;
  else x^=y;
  return x;
}

void Main() {
  while(cin >> s && s!=".") {
    cin >> t;
    k=0;
    int x=dfs();
    int ans=0;
    rep(i,10000) {
      stringstream ss;
      ss << i;
      ss >> t;
      while(t.size()<4) t='0'+t;
      k=0;
      if(dfs()==x) ans++;
    }
    pr(x,ans);
  }
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include <boost/multiprecision/cpp_int.hpp>
//typedef boost::multiprecision::cpp_int ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E17;

#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
void Max(ll &pos,ll val){pos=max(pos,val);}//Max(dp[n],dp[n-1]);
void Min(ll &pos,ll val){pos=min(pos,val);}
void Add(ll &pos,ll val){pos=mod(pos+val);}
dd EPS=1E-9;
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
///////////////////////////////////////

typedef string::const_iterator State;
string s;
ll t[4];

ll has(State &begin);
ll let(State &begin){
    if(*begin=='a'){begin++;
        return t[0];
    }else if(*begin=='b'){begin++;
        return t[1];
    }
    ll k=*begin-'a';
    begin++;
    return t[k];
}
string ope="+*^";
ll has(State &begin){
    if(*begin=='['){
        begin++;
        ll op=-1;
        rep(i,0,2)if(*begin==ope[i])op=i;
        begin++;
        ll a=has(begin);
        ll b=has(begin);
        begin++;
        if(op==0){
            return a|b;
        }else if(op==1){
            return a&b;
        }else{
            return a^b;
        }
    }else{
        return let(begin);
    }
}

int main(){fastio
    
    while(1){
        cin>>s;
        if(s[0]=='.')break;
        ll ans[10000];memset(ans,-1,sizeof(ans));
        
        rep(i,0,9999){
            State begin=s.begin();
            ll ni=i;
            for(ll j=3;j>=0;j--){
                t[j]=ni%10;
                ni/=10;
            }
            ans[i]=has(begin);
        }
        
        ll k;cin>>k;
        ll p=ans[k];
        ll c=0;rep(i,0,9999)if(ans[i]==p)c++;
        cout<<p<<" "<<c<<endl;
    }
    return 0;
}