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 "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

void func(vector<char>&op, vector<vector<int>>&edge, string &s, int& place , int node = 0) {
	if (s[place] == '+') {
		op.push_back(s[place]);
		edge.push_back(vector<int>(2));
	}
	if (s[place] == '*') {
		op.push_back(s[place]);
		edge.push_back(vector<int>(2));
	}
	if (s[place] == '^') {
		op.push_back(s[place]);
		edge.push_back(vector<int>(2));
	}
	place++;
	for (int loop = 0; loop < 2; loop++) {
		if (s[place] == '[') {
			edge[node][loop] = edge.size();
			place++;
			func(op, edge, s, place, edge.size());
		}
		else {
			edge[node][loop] = s[place] - 'e';
			place++;
		}
	}
	if (s[place] == ']') {
		place++;
		return;
	}
}

int ans(vector<char>&op, vector<vector<int>>&edge,vector<int>&num, int node=0) {
	int a, b;
	if (edge[node][0] < 0) {
		a = num[-edge[node][0]];
	}
	else {
		a = ans(op, edge, num, edge[node][0]);
	}
	if (edge[node][1] < 0) {
		b = num[-edge[node][1]];
	}
	else {
		b = ans(op, edge, num, edge[node][1]);
	}
	if (op[node] == '+')return a | b;
	if (op[node] == '*')return a & b;
	return a ^ b;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	string s;
	while (cin >> s, s != ".") {
		cin >> N;
		vector<int>v(10000);
		if (s.size() == 1) {
			if (s[0]=='a')cout << N / 1000;
			if (s[0] == 'b')cout << (N / 100) % 10;
			if (s[0] == 'c')cout << (N / 10) % 10;
			if (s[0] == 'd')cout << (N / 1) % 10;
			cout << " " << 1000 << endl;
			continue;
		}
		s.erase(s.begin());
		vector<char>op;
		vector<vector<int>>edge;
		int cnt = 0;
		func(op, edge, s, cnt);
		for (int i = 0; i < 10000; i++) {
			int box = i;
			vector<int>num(5);
			for (int j = 1; j <= 4; j++) {
				num[j] = box % 10;
				box /= 10;
			}
			v[i] = ans(op, edge, num);
		}
		int ret = 0;
		for (int i = 0; i < 10000; i++)ret += v[N] == v[i];
		cout << v[N] << " " << ret << endl;
	}
}

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 main(){
  int n;
  string str;
  stack<int>s;
  stack<char>op;
  char e;
  int x,y,z;
  int cnt=0;

  while(1)
    {
      cin >> str;
      if(str == "-") break;
      cin >> n;

      int a,b,c,d;
      a = n/1000;
      b = (n%1000)/100;
      c = (n%100)/10;
      d = n%10;

      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));
	    }
	}
      cout << s.top() << " ";
      int z = s.top();
      
      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++){
	      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));
	    }
	}
	      if(z == s.top())cnt++;
	    }
	  }
	}
      }
      cout << cnt << endl;
    }
  return 0;
}