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;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR (i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {if (a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmin(A &a, B b) {if (a > b) {a = b; return 1;} return 0;}
typedef long long ll;
typedef pair<int, int> pii;
const ll INF = 1e9+100;
const bool debug = 0;
/****************************/

struct SyntaxAnalysis {
	string s;
	int i;
	int val;
	
	int init(string _s) {
		s = _s;
		i = 0;
		
		val = expr(i);
		return val;
	}
	
	int expr(int &i) {
		int res = 0;
		if (s[i] >= '0' && s[i] <= '9') {
			res = s[i] - '0';
		}
		else {
			res = term(++i);
			++i;
		}
		return res;
	}
	
	int term(int &i) {
		int ope;
		if (s[i] == '+') ope = 0;
		else if (s[i] == '*') ope = 1;
		else ope = 2;
		
		int val1 = expr(++i);
		int val2 = expr(++i);
		
		int res;
		if (ope == 0) res = val1 | val2;
		else if (ope == 1) res = val1 & val2;
		else res = val1 ^ val2;
		return res;
	}
};

string make(int i) {
	string num = to_string(i);
	reverse(ALL(num));
	while (num.size() < 4) num += '0';
	reverse(ALL(num));
	return num;
}

string arrange(string str, string num) {
	REP(i, str.size()) {
		if (str[i] >= 'a' && str[i] <= 'd') {
			str[i] = num[str[i]-'a'];
		}
	}
	return str;
}

int main() {
	string s;
	while (cin >> s, s != ".") {
		string num;
		cin >> num;
		
		string tmp = arrange(s, num);
		
		SyntaxAnalysis sa;
		int hsorg = sa.init(tmp);
		
		int ans = 0;
		REP(i, 10000) {
			string cur = make(i);
			if (hsorg == sa.init(arrange(s, cur))) {
				ans++;
			}
		}
		
		if (debug) printf("@ ");
		cout << hsorg << " " << ans << endl;
	}
	return 0;
}

// C

C++: #include <bits/stdc++.h>
using namespace std;
int eval(string S, string P){
  int N = S.size();
  while (P.size() != 4){
    P = '0' + P;
  }
  for (int i = 0; i < N; i++){
    if ('a' <= S[i] && S[i] <= 'd'){
      S[i] = P[S[i] - 'a'];
    }
  }
  stack<int> st;
  for (int i = 0; i < N; i++){
    stack<int> st2 = st;
    if ('0' <= S[i] && S[i] <= '9'){
      st.push(S[i] - '0');
    } else {
      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;
    cin >> S;
    if (S == "."){
      break;
    }
    string P;
    cin >> P;
    reverse(S.begin(), S.end());
    int N = S.size();
    string T;
    for (int i = 0; i < N; i++){
      if (S[i] != '[' && S[i] != ']'){
        T += S[i];
      }
    }
    int ans1 = eval(T, P);
    int ans2 = 0;
    for (int i = 0; i < 10000; i++){
      if (eval(T, to_string(i)) == ans1){
        ans2++;
      }
    }
    cout << ans1 << ' ' << ans2 << endl;
  }
}

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

string s;
int a[4];

int expr(string &s,int &p){
  //cout<<s<<":"<<p<<endl;
  if(isalpha(s[p])){
    return a[s[p++]-'a'];
  }
  assert(s[p]=='[');
  p++;
  char op=s[p++];
  int x=expr(s,p);
  int y=expr(s,p);
  assert(s[p]==']');
  p++;
  if(op=='+') return x|y;
  if(op=='*') return x&y;
  if(op=='^') return x^y;
  assert(0);
}


signed main(){
  while(cin>>s,s!="."){
    string x;
    cin>>x;
    //cout<<s<<":"<<x<<endl;
    int cnt[16]={};
    for(a[0]=0;a[0]<10;a[0]++){
      for(a[1]=0;a[1]<10;a[1]++){
	for(a[2]=0;a[2]<10;a[2]++){
	  for(a[3]=0;a[3]<10;a[3]++){
	    int p=0;
	    cnt[expr(s,p)]++;
	  }
	}
      }
    }
    for(int i=0;i<4;i++) a[i]=x[i]-'0';
    int p=0;
    int k=expr(s,p);
    cout<<k<<" "<<cnt[k]<<endl;
  }
  return 0;
}