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 lp(i,n) for(int i=0;i<n;i++)

int enzan(int num1,int num2,int clc){
  int numa[4];
  int numb[4];
  int ans[4];
  lp(i,4){
    numa[i]=0;
    numb[i]=0;
  }
  if(num1>=8){
    numa[0]=1;
    num1-=8;
  }
  if(num1>=4){
    numa[1]=1;
    num1-=4;
  }
  if(num1>=2){
    numa[2]=1;
    num1-=2;
  }
  if(num1>=1){
    numa[3]=1;
    num1-=1;
  }
  if(num2>=8){
    numb[0]=1;
    num2-=8;
  }
  if(num2>=4){
    numb[1]=1;
    num2-=4;
  }
  if(num2>=2){
    numb[2]=1;
    num2-=2;
  }
  if(num2>=1){
    numb[3]=1;
    num2-=1;
  }
  //  lp(i,4){
  //  cout<<numa[i];
  //}
  //cout<<endl;
  //lp(i,4){
  //  cout<<numb[i];
  //}
  //cout<<endl;

  lp(i,4){
    if(clc==1){
      if(numa[i]==1&&numb[i]==1) ans[i]=1;
      else ans[i]=0;
    }
    if(clc==2){
      if(numa[i]==numb[i]) ans[i]=0;
      else ans[i]=1;
    }
    if(clc==3){
      if(numa[i]==0&&numb[i]==0) ans[i]=0;
      else ans[i]=1;
    }
  }
  return ans[0]*8+ans[1]*4+ans[2]*2+ans[3]*1;
}


int change(string calc){
  while(1){
    int num=0,ca=0;
    int memo[2];
    //int hoge;
    //cout<<calc<<endl;
    //cin>>hoge;
    if(calc.size()==1) break;
    for(int i=0;i<calc.size();i++){
      if(calc[i]=='['){
	num=0;
	ca=0;
      }
      if(calc[i]=='*') ca=1;
      if(calc[i]=='^') ca=2;
      if(calc[i]=='+') ca=3;
      if('0'<=calc[i]&&calc[i]<='9'){
	memo[num]=calc[i]-'0';
	num++;
      }
      if('a'<=calc[i]&&calc[i]<='z'){
	memo[num]=calc[i]-'a'+10;
	num++;
      }
      if(calc[i]==']'){
	if(num==1){
	  calc.erase(i,1);
	  calc.erase(i-2,1);
	  break;
	}
	else if(num==2){
	  int memori=enzan(memo[0],memo[1],ca);
	  if(memori<=9){
	    calc[i-1]=memori+'0';
	    calc.erase(i-2,1);
	    calc.erase(i-3,1);
	  }
	  else{
	    calc[i-1]=memori-10+'a';
	    calc.erase(i-2,1);
	    calc.erase(i-3,1);
	  }
	  break;
	}
      }
    }
  }
  if('a'<=calc[0]&&calc[0]<='z') return calc[0]-'a'+10;
  return calc[0]-'0';
      if(mems==0) cout<<i<<endl;
}


int main(){
  while(1){
    string cast;
    cin>>cast;
    if(cast==".") break;
    int mez;
    cin>>mez;
    int xx[20];
    lp(i,20) xx[i]=0;
    int ansmemo;
    for(int i=0;i<10000;i++){
      string calc=cast;
      char a,b,c,d;
      a='0'+i/1000;
      b='0'+i%1000/100;
      c='0'+i%100/10;
      d='0'+i%10;
      lp(j,calc.size()){
	if(calc[j]=='a'){
	  calc[j]=a;
	}
	if(calc[j]=='b'){
	  calc[j]=b;
	}
	if(calc[j]=='c'){
	  calc[j]=c;
	}
	if(calc[j]=='d'){
	  calc[j]=d;
	}
      }
      int mems=change(calc);
      if(mez==i) ansmemo=mems;
      xx[mems]++;
    }
    cout<<ansmemo<<" "<<xx[ansmemo]<<endl;
  }
  return 0;
}

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

using namespace std;

#define endl "\n"
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define rrep(i, a, b) for (auto i = (a); i > (b); --i)

int pass[4];

int sol(string &s)
{
  int len = (int)s.size();
  int rank = 0;
  vector <int> v[100];
  rrep(i, len - 1, -1) {
    int tmp;
    if (s[i] == ']') {rank++; continue;}
    else if (s[i] == '[') {rank--; continue;}
    else if (s[i] == '^') {
      tmp = v[i][0];
      rep(j, 1, (int)v[i].size()) tmp ^= v[i][j];
    } else if (s[i] == '+') {
      tmp = v[i][0];
      rep(j, 1, (int)v[i].size()) tmp |= v[i][j];
    } else if (s[i] == '*') {
      tmp = v[i][0];
      rep(j, 1, (int)v[i].size()) tmp &= v[i][j];
    } else if (s[i] == 'a') tmp = pass[0];
    else if (s[i] == 'b') tmp = pass[1];
    else if (s[i] == 'c') tmp = pass[2];
    else tmp = pass[3];
    if (i == 0) {
      return tmp;
    }
    int rtmp = rank;
    int rgoal = (s[i] == '*' || s[i] == '^' || s[i] == '+' ? rank - 1 : rank);
    rrep(j, i - 1, -1) {
      if (s[j] == ']') rtmp++;
      else if (s[j] == '[') rtmp--;
      if (rgoal == rtmp && (s[j] == '^' || s[j] == '*' || s[j] == '+')) {
        v[j].push_back(tmp);
        break;
      }
      if (j == 0) {
        return tmp;
      }
    }
  }
  return 0;
}

int main()
{
  string s;
  string n;
  while (cin >> s)
  {
    if (s == ".")
      return 0;
    cin >> n;
    rep(i, 0, (int)n.size())
    {
      pass[i] = n[i] - '0';
    }
    int tmp = sol(s);
    int ans = 0;
    rep(i, 0, 10) rep(j, 0, 10) rep(k, 0, 10) rep(l, 0, 10) {
      pass[0] = i, pass[1] = j, pass[2] = k, pass[3] = l;
      if (tmp == sol(s)) {
        ans++;
      }
    }
    cout << tmp << " " << ans << endl;
  }
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a) (a).begin(), (a).end()
#define pb emplace_back
#define INF (1e9+1)

string s;

int hf(int &p, string &id){
    if('a'<=s[p] && s[p]<='d'){
        int ret = id[s[p]-'a']-'0';
        p++;
        return ret;

    }else{
        assert('[');
        p++;
        char op = s[p];
        p++;
        int left = hf(p,id);
        int right = hf(p,id);
        assert(']');
        p++;

        if(op=='*')return left & right;
        if(op=='+')return left | right;
        if(op=='^')return left ^ right;
    }
    abort();
    return -1;
}

int main(){
    while(cin>>s&&s!="."){
        string id;
        cin>>id;

        int num = 0;
        int res = hf(num,id);

        int sum = 0;
        rep(i,10000){
            stringstream ss;
            ss<<i;
            string t = ss.str();
            while(t.size()<4)t = "0"+t;
            int num = 0;
            if(res==hf(num,t))sum++;
        }
        cout<<res<<" "<<sum<<endl;
    }
}