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>
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const string space=" ";
int f(string s,int p[]){
	stack<int> st;
	for(int i=s.size()-1;i>=0;i--){
		if('a'<=s[i]&&s[i]<='d'){
			st.push(p[s[i]-'a']);
			continue;
		}
		if(s[i]=='['||s[i]==']')continue;
		int a=st.top();
		st.pop();
		int b=st.top();
		st.pop();
		if(s[i]=='+')
			st.push(a|b);
		if(s[i]=='*')
			st.push(a&b);
		if(s[i]=='^')
			st.push(a^b);
	}
	return st.top();
}
int main(){
	while(1){
		string s,p;
		cin>>s;
		if(s==".")break;
		cin>>p;
		int a[4];
		rep(i,4)a[i]=p[i]-'0';
		int h=f(s,a);
		int ans=0;
		rep(i,10){
			rep(j,10){
				rep(k,10){
					rep(l,10){
						a[0]=i;
						a[1]=j;
						a[2]=k;
						a[3]=l;
						int b=f(s,a);
						ans+=h==b;
					}
				}
			}
		}
		cout<<h<<space<<ans<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i))
#define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template<class T> bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T> bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}
template<class T> T div_floor(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>=0 ? a/b : (a+1)/b-1;
}
template<class T> T div_ceil(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>0 ? (a-1)/b+1 : a/b;
}

constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 100010;

int calc(int lhs, int rhs, char op){
    if(op == '+') return lhs | rhs;
    else if(op == '*') return lhs & rhs;
    else return lhs ^ rhs;
}

int expr(string &s, int &i, string &p);
int number(string &s, int &i, string &p);

int expr(string &s, int &i, string &p){
    if(s[i] == '['){
        ++i;
        char op = s[i];
        ++i;
        int lhs = expr(s, i, p), rhs = expr(s, i, p);
        ++i;
        return calc(lhs, rhs, op);
    }
    return number(s, i, p);
}

int number(string &s, int &i, string &p){
    int ret = p[s[i]-'a'] - '0';
    ++i;
    return ret;
}

void dfs(int cur, int tmp, int &cnt, string &t, string &s){
    if(cur == 4){
        int i = 0;
        cnt += (expr(s, i, t) == tmp);
        return;
    }
    for(char c='0'; c<='9'; ++c){
        t += c;
        dfs(cur + 1, tmp, cnt, t, s);
        t.pop_back();
    }
}

int main(){
    string s, p;
    while(cin >> s && s != "."){
        cin >> p;
        int i = 0;
        int tmp = expr(s, i, p);
        int cnt = 0;
        string t;
        dfs(0, tmp, cnt, t, s);
        printf("%d %d\n", tmp, cnt);
    }
}

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;
    }
}

C++: #include <iostream>
using namespace std;

string replace(string s, string p){
  for(size_t i = 0; i < s.length(); ++i){
    if(s[i] >= 'a' && s[i] < 'e'){
      s[i] = p[s[i]-'a'];
    }
  }
  return s;
}

int Hash(int &i, string &s){
  if(s[i] == '['){
    ++i;
    int ret = Hash(i,s);
    ++i;
    return ret;
  }
  if(s[i] == '+'){
    ++i;
    int a = Hash(i,s), b = Hash(i,s);
    return (a|b);
  }
  if(s[i] == '*'){
    ++i;
    int a = Hash(i,s), b = Hash(i,s);
    return (a&b);
  }
  if(s[i] == '^'){
    ++i;
    int a = Hash(i,s), b = Hash(i,s);
    return (a^b);
  }
  if(s[i] >= '0' && s[i] <= '9'){
    ++i;
    return (s[i-1] - '0');
  }
  return 0;
}

void solve(string s, string p){
  string s_ = replace(s,p);
  //cout << s_ << endl;
  int i = 0, t = Hash(i,s_), count = 0;
  for(int a = 0; a < 10; ++a){
    for(int b = 0; b < 10; ++b){
      for(int c = 0; c < 10; ++c){
        for(int d = 0; d < 10; ++d){
          string p_ = to_string(a) + to_string(b) + to_string(c) + to_string(d);
          i = 0;
          s_ = replace(s,p_);
          if(Hash(i,s_) == t) ++count;
        }
      }
    }
  }
  cout << t << " " << count << endl;
}

int main(){
  string s;
  while(cin >> s, s != "."){
    string p;
    cin >> p;
    solve(s,p);
  }
  return 0;
}