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 <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#include <time.h>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector< vector<ll> > matrix;
#define REP(i, x, y) for(ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for(ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for(ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for(ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;
void yesno(bool ok, string yes = "Yes", string no = "No") {
    if (ok) {
        cout << yes << endl;
    } else {
        cout << no << endl;
    }
}

string s;
ll cur = 0;

ll hashf(ll a, ll b, ll c, ll d) {
    if (s[cur] == 'a') {
        cur++;
        return a;
    } else if (s[cur] == 'b') {
        cur++;
        return b;
    } else if (s[cur] == 'c') {
        cur++;
        return c;
    } else if (s[cur] == 'd') {
        cur++;
        return d;
    }
    assert(s[cur] == '[');
    cur++;
    char op = s[cur];
    cur++;
    ll p = hashf(a, b, c, d);
    ll q = hashf(a, b, c, d);
    assert(s[cur] == ']');
    cur++;
    if (op == '+') {
        return p | q;
    } else if (op == '*') {
        return p & q;
    } else {
        return p ^ q;
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    while (1) {
        cin >> s;
        if (s == ".") {
            break;
        }
        ll num;
        cin >> num;
        ll numd = num % 10;
        num /= 10;
        ll numc = num % 10;
        num /= 10;
        ll numb = num % 10;
        num /= 10;
        ll numa = num;
        cur = 0;
        ll hs = hashf(numa, numb, numc, numd);
        ll ans = 0;
        REP(i, 0, 10) {
            REP(j, 0, 10) {
                REP(k, 0, 10) {
                    REP(l, 0, 10) {
                        cur = 0;
                        ll tmp = hashf(i, j, k, l);
                        if (hs == tmp) {
                            ans++;
                        }
                    }
                }
            }
        }
        cout << hs << " " << ans << endl;
    }
}

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  
  while (true) {
    string s;
    cin >> s;
    if (s == ".") return 0;
    
    reverse(s.begin(), s.end());
    
    int p;
    cin >> p;
    
    int hash;
    map<int, int> mp;
    for (int num = 0; num < 10000; num++) {
      vector<int> x(4);
      int tmp = num;
      for (int i = 3; i >= 0; i--) {
        x.at(i) = tmp % 10;
        tmp /= 10;
      }
      
      stack<int> st;
      for (int i = 0; i < s.size(); i++) {
        if (s[i] == '[' || s[i] == ']') continue;
        if ('a' <= s[i] && s[i] <= 'd') st.push(x[s[i] - 'a']);
        
        if (s[i] == '+') {
          int k1 = st.top();
          st.pop();
          int k2 = st.top();
          st.pop();
          int k3 = (k1 | k2);
          st.push(k3);
        }
        if (s[i] == '*') {
          int k1 = st.top();
          st.pop();
          int k2 = st.top();
          st.pop();
          int k3 = (k1 & k2);
          st.push(k3);
        }
        if (s[i] == '^') {
          int k1 = st.top();
          st.pop();
          int k2 = st.top();
          st.pop();
          int k3 = (k1 ^ k2);
          st.push(k3);
        }
      }
      
    mp[st.top()]++;
    if (p == num) hash = st.top();
    }
    
    cout << hash << " " << mp[hash] << '\n';
  }
}