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 <stdio.h>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void pul(int p);
void kake(int p);
void haita(int p);
void han(int p);


string s,p;
int ansa;
int ansb;
vector<int> ppp;

int main (){
  cin >> s;
  while(s!="."){
    ppp.assign(80,-10);
    ansa=0;
    ansb=0;
    int a,b,c,d;
    cin >> p;
    a=int(p[0])-'0';
    b=int(p[1])-'0';
    c=int(p[2])-'0';
    d=int(p[3])-'0';
    for(int t=0;t<s.size();++t){
      if(s[t]=='['){
        ppp[t]=-1;
      }
      else if(s[t]==']'){
        ppp[t]=-2;
      }
      else if(s[t]=='+'){
        ppp[t]=-3;
      }
      else if(s[t]=='*'){
        ppp[t]=-4;
      }
      else if(s[t]=='^'){
        ppp[t]=-5;
      }
      else{
        switch(s[t]){
          case 'a':
            ppp[t]=a;
            break;
          case 'b':
            ppp[t]=b;
            break;
          case 'c':
            ppp[t]=c;
            break;
          case 'd':
            ppp[t]=d;
            break;
        }
      }
    }
    switch(ppp[0]){
      case 0:
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
      case 8:
      case 9:
        break;
      default:
        han(1);
        break;
    }
    ansa=ppp[0];
    for(a=0;a<10;++a){
      for(b=0;b<10;++b){
        for(c=0;c<10;++c){
          for(d=0;d<10;++d){
            ppp.assign(80,-10);
    for(int t=0;t<s.size();++t){
      if(s[t]=='['){
        ppp[t]=-1;
      }
      else if(s[t]==']'){
        ppp[t]=-2;
      }
      else if(s[t]=='+'){
        ppp[t]=-3;
      }
      else if(s[t]=='*'){
        ppp[t]=-4;
      }
      else if(s[t]=='^'){
        ppp[t]=-5;
      }
      else{
        switch(s[t]){
          case 'a':
            ppp[t]=a;
            break;
          case 'b':
            ppp[t]=b;
            break;
          case 'c':
            ppp[t]=c;
            break;
          case 'd':
            ppp[t]=d;
            break;
        }
      }
    }
    switch(ppp[0]){
      case 0:
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
      case 8:
      case 9:
        break;
      default:
        han(1);
        break;
    }
    if(ppp[0]==ansa){
      ++ansb;
    }

          }
        }
      }
    }



    printf("%d %d\n",ansa,ansb);
    cin >> s;
  }
  /*for(int t=0;t<q;++t){
    printf("%d\n",qqq[t]);
  }*/
}

void pul(int p){
  if(ppp[p]==-1){
    han(p+1);
  }
  if(ppp[p+1]==-1){
    han(p+2);
  }
  ppp[p-2]=ppp[p]|ppp[p+1];
  ppp.erase(ppp.begin()+p-1,ppp.begin()+p+3);
}

void kake(int p){
  if(ppp[p]==-1){
    han(p+1);
  }
  if(ppp[p+1]==-1){
    han(p+2);
  }
  ppp[p-2]=ppp[p]&ppp[p+1];
  ppp.erase(ppp.begin()+p-1,ppp.begin()+p+3);
}

void haita(int p){
  if(ppp[p]==-1){
    han(p+1);
  }
  if(ppp[p+1]==-1){
    han(p+2);
  }
  ppp[p-2]=ppp[p]^ppp[p+1];
  ppp.erase(ppp.begin()+p-1,ppp.begin()+p+3);
}

void han(int p){
  switch(ppp[p]){
    case -5:
      haita(p+1);
      break;
    case -4:
      kake(p+1);
      break;
    case -3:
      pul(p+1);
      break;
  }
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <algorithm>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ll long long
#define lld long double
#define ALL(x) x.begin(), x.end()
#ifdef DEBUG
#define line() cerr << "[" << __LINE__ << "] ";
#define dump(i) cerr << #i ": " << i << " ";
#define dumpl(i) cerr << #i ": " << i << endl;
#else
#define line(i)
#define dump(i)
#define dumpl(i)
#endif
using namespace std;

typedef string::const_iterator State;

map<char, int> mp;
int hashval(State &);
int op(State &);
int letter(State &);

class ParseError
{
};
void consume(State &begin, char expected)
{
    if (*begin == expected)
    {
        begin++;
    }
    else
    {
        cerr << "Expected '" << expected << "' but got '" << *begin << "'"
             << endl;
        cerr << "Rest string is '";
        while (*begin)
        {
            cerr << *begin++;
        }
        cerr << "'" << endl;
        throw ParseError();
    }
}
int hashval(State &begin)
{
    if (*begin == '[')
    {
        consume(begin, '[');
        int ret = op(begin);
        consume(begin, ']');
        return ret;
    }
    else
    {
        return letter(begin);
    }
}
int op(State &begin)
{
    char op = *begin;
    begin++;
    int a = hashval(begin);
    int b = hashval(begin);

    if (op == '+')
    {
        return a | b;
    }
    if (op == '*')
    {
        return a & b;
    }
    if (op == '^')
    {
        return a ^ b;
    }
}
int letter(State &begin)
{
    int ret = mp[*begin];
    begin++;
    return ret;
}

void setmap(int t)
{
    rep(i, 4)
    {
        mp['d' - i] = t % 10;
        t /= 10;
    }
}

int main()
{

    while (true)
    {
        string s;
        int t;
        cin >> s;
        if (s == ".")
            break;
        State begin = s.begin();

        cin >> t;
        setmap(t);
        int ans = 0;

        int enc = hashval(begin);
        rep(i, 9999 + 1)
        {
            begin = s.begin();
            setmap(i);
            if (hashval(begin) == enc)
                ans++;
        }
        cout << enc << " " << ans << endl;
    }
}