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 long long ll;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
#define fastio ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);
/////////////////////////////

ll cal(vector<ll>s){
    ll ls=s.size();
    while(s.size()>=2){
        
        ls=s.size();
        bool debugflag=false;
        vector<ll>t;
        rep(i,0,ls-1){
            bool flag=false;
            if(i<=ls-5)if(s[i]==100&&s[i+4]==200){
                if(0<=s[i+2]&&s[i+2]<=15){
                    if(0<=s[i+3]&&s[i+3]<=15){
                        ll add=0;
                        if(s[i+1]==300){
                            add=(s[i+2]|s[i+3]);
                            flag=true;
                            
                        }else if(s[i+1]==400){
                            add=(s[i+2]^s[i+3]);
                            flag=true;
                        }else if(s[i+1]==500){
                            add=(s[i+2]&s[i+3]);
                            flag=true;
                        }
                        
                        if(flag){
                            t.pb(add);
                        }
                    }
                }
            }
            if(!flag){
                t.pb(s[i]);
            }else{
                i+=4;
                debugflag=true;
            }
        }
        /*if(debugflag==false){
            for(auto x:s){
                cout<<x<<" ";
            }cout<<endl;
            return -1;
        }*/
        s.clear();
        for(auto x:t)s.pb(x);
    }
    return s[0];
}

bool ansproblem(string moto){
    string t;cin>>t;
    vector<ll>s;
    ll ls=moto.length();
    rep(i,0,ls-1){
        if(moto[i]=='[')s.pb(100);
        else if(moto[i]==']')s.pb(200);
        else if(moto[i]=='+')s.pb(300);
        else if(moto[i]=='^')s.pb(400);
        else if(moto[i]=='*')s.pb(500);
        else{
            if(moto[i]=='a')s.pb(t[0]-'0');
            else if(moto[i]=='b')s.pb(t[1]-'0');
            else if(moto[i]=='c')s.pb(t[2]-'0');
            else s.pb(t[3]-'0');
        }
    }
    ll ans=cal(s);
    ll count=0;
    rep(a,0,9){
        rep(b,0,9){
            rep(c,0,9){
                rep(d,0,9){
                    vector<ll>s;
                    rep(i,0,ls-1){
                        if(moto[i]=='[')s.pb(100);
                        else if(moto[i]==']')s.pb(200);
                        else if(moto[i]=='+')s.pb(300);
                        else if(moto[i]=='^')s.pb(400);
                        else if(moto[i]=='*')s.pb(500);
                        else{
                            if(moto[i]=='a')s.pb(a);
                            else if(moto[i]=='b')s.pb(b);
                            else if(moto[i]=='c')s.pb(c);
                            else s.pb(d);
                        }
                    }
                    if(cal(s)==ans)count++;
                }
            }
        }
    }
    cout<<ans<<" "<<count<<endl;
    
    return 0;
}

int main(){fastio
    
    while(1){
        string s;cin>>s;
        if(s[0]=='.'){
            break;
        }
        ansproblem(s);
    }
    return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
using namespace std;
#define INF 0x7FFFFFFF
#define ll long long

int get_ans(string t);
int get_num();

string s,ss,t,tt;
vector<int> a;
int k;

//  [ -1
//  ] -2
//  + -3
//  * -4
//  ^ -5

int main(){
  cin >> ss;
  while(ss!="."){
    k=0;
    cin >> tt;
    s=ss;
    t=tt;
    int num=get_ans(t);
    int ans=0;
    t="0000";
    for(int i=0;i<10000;i++){
      k=0;
      if(get_ans(t)==num){
        ans++;
      }
      t[0]++;
      for(int l=0;l<3;l++){
        if(t[l]>'9'){
          t[l]-=10;
          t[l+1]++;
        }
      }
    }
    cout << num << " " << ans << endl;
    cin >> ss;
  }
}

int get_ans(string t){
  a.assign(s.size(),0);
  for(int i=0;i<(int)s.size();i++){
    if('a'<=s[i]&&s[i]<='d'){
      a[i]=t[s[i]-'a']-'0';
    }
    else{
      switch(s[i]){
        case '[':
          a[i]=-1;
          break;
        case ']':
          a[i]=-2;
          break;
        case '+':
          a[i]=-3;
          break;
        case '*':
          a[i]=-4;
          break;
        case '^':
          a[i]=-5;
          break;
      }
    }
  }
  return get_num();
}


int get_num(){
  if(a[k]>=0){
    k++;
    return a[k-1];
  }
  else if(a[k]==-1){
    k++;
    int x;
    int y;
    switch(a[k]){
      case -3:
        k++;
        x=get_num();
        y=get_num();
        k++;
        return x|y;
        break;
      case -4:
        k++;
        x=get_num();
        y=get_num();
        k++;
        return x&y;
        break;
      case -5:
        k++;
        x=get_num();
        y=get_num();
        k++;
        return x^y;
        break;
    }
  }
  return 0;
}