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 REP(i, n) for (int i=0; i<(n); ++i)
#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)
#define FOR(i, a, n) for (int i=(a); i<(n); ++i)
#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()

#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;

template<class T>
ostream &operator<<(ostream &os, const vector <T> &v) {
    os << "[";
    REP(i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}

template<class T, class U>
ostream &operator<<(ostream &os, const pair <T, U> &p) {
    return os << "(" << p.first << " " << p.second << ")";
}

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 (b < a) {
        a = b;
        return true;
    }
    return false;
}

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;

template<typename T>
struct edge {
    int src, to;
    T cost;
};

template<typename T>
using Graph = vector<vector<edge<T>>>;

using State = string::const_iterator;

int parseHash(State &begin, int n) {
    if (*begin == '[') {
        ++begin;
        char op = *begin;
        ++begin;
        int left = parseHash(begin, n), right = parseHash(begin, n);
        assert(*begin == ']');
        ++begin;
        if (op == '+') {
            return left | right;
        }
        if (op == '*') {
            return left & right;
        }
        if (op == '^') {
            return left ^ right;
        }
    }
    if (*begin == 'a') {
        ++begin;
        return n / 1000 % 10;
    }
    if (*begin == 'b') {
        ++begin;
        return n / 100 % 10;
    }
    if (*begin == 'c') {
        ++begin;
        return n / 10 % 10;
    }
    if (*begin == 'd') {
        ++begin;
        return n / 1 % 10;
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    // ifstream in("in.txt");
    // cin.rdbuf(in.rdbuf());

    for (;;) {
        string s;
        cin >> s;
        if (s == ".") break;

        map<int, int> cnt;
        REP(i, 10000) {
            State begin = s.begin();
            ++cnt[parseHash(begin, i)];
        }

        State begin = s.begin();
        int p;
        cin >> p;
        int hash = parseHash(begin, p);

        cout << hash << ' ' << cnt[hash] << endl;
    }

    return 0;
}

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

int bin(int x,int y,char e)
{
  if(e == '+')
    {
      return x | y;
    }
  else if(e == '*')
    {
      return x & y;
    }
  else
    {
      return x ^ y;
    }
}
int compute(const string &str,int a,int b,int c,int d){
  
  stack<int>s;
  stack<char>op;
  char e;
  int x,y,z;
  for(int i=0;i < str.size();i++)
    {
      if(str[i] == 'a')
	{
	  s.push(a);
	}
      else if(str[i] == 'b')
	{
	  s.push(b);
	}
      else if(str[i] == 'c')
	{
	  s.push(c);
	}
      else if(str[i] == 'd')
	{
	  s.push(d);
	}
      
      if(str[i] == '+')
	{
	  op.push('+');
	}
      else if(str[i] == '*')
	{
	  op.push('*');
	}
      else if(str[i] == '^')
	{
	  op.push('^');
	}

      if(str[i] == ']')
	{
	  e = op.top();
	  op.pop();
	  x = s.top();
	  s.pop();
	  y = s.top();
	  s.pop();
	  s.push(bin(x,y,e));
	}
    }
  return s.top();
}

int main(){
  int n;
  string str;
  int cnt=0;

  while(1){
    cnt =0;
    cin >> str;
    if(str == ".") break;
    cin >> n;

    int key = compute(str, n/1000, n/100%10, n/10%10, n%10 );
    
    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++){
	    if( compute(str,a,b,c,d) == key){
	      cnt++;
	    }
	  }//d
	}//c
      }//b
    }//a
    cout << key << " " << cnt << endl;

    
  }// while
  
  return 0;
}

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

class Solve {
private:
	std::string hashFunc;
	int password[4]{}, hash_value;

	int calcHash(int& s_i)
	{
		int ret{};
		if (hashFunc[s_i] == '[')
		{
			s_i++;
			char ope{hashFunc[s_i]};
			s_i++;
			int lhs{calcHash(s_i)};
			int rhs{calcHash(s_i)};
			if (ope == '+') ret =  lhs | rhs;
			else if (ope == '^') ret =  lhs ^ rhs;
			else ret = lhs & rhs;
			s_i++;
		}
		else
		{
			ret = password[hashFunc[s_i] - 'a'];
			s_i++;
		}
		return ret;
	}

	int rc(int index)
	{
		if (index == 4)
		{
			int s_i{};
			return calcHash(s_i) == hash_value;
		}
		int ret{};
		for (int i{}; i < 10; i++)
		{
			password[index] = i;
			ret += rc(index + 1);
		}
		return ret;
	}

public:
	bool is_last_query{};
	Solve()
	{
		std::cin >> hashFunc;
		if (hashFunc == ".")
		{
			is_last_query = true;
			return;
		}
		for (int i{}; i < 4; i++)
		{
			char tmp;
			scanf(" %c", &tmp);
			password[i] = tmp - '0';
		}
		int s_i{};
		hash_value = calcHash(s_i);
		printf("%d %d\n", hash_value, rc(0));
	}
};

int main()
{
	while (!Solve().is_last_query);

	return 0;
}