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 <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int solve(string &s, array<int, 4> &v){
    stack<int> S;
    for (auto &&i : s) {
        if(i == '[' || i == ']') continue;
        if(isalpha(i)){
            S.emplace(v[i-'a']);
        }else {
            int A = S.top(); S.pop();
            int B = S.top(); S.pop();
            if(i == '+') S.emplace(A|B);
            else if(i == '*') S.emplace(A&B);
            else if(i == '^') S.emplace(A^B);
        }
    }
    return S.top();
}

int main() {
    string s;
    while(cin >> s, s != "."){
        string t;
        cin >> t;
        array<int, 4> v{};
        for (int i = 0; i < 4; ++i) {
            v[i] = t[i]-'0';
        }
        reverse(s.begin(),s.end());
        int x = solve(s, v);
        int ans = 0;
        for (int i = 0; i < 10; ++i) {
            for (int j = 0; j < 10; ++j) {
                for (int k = 0; k < 10; ++k) {
                    for (int l = 0; l < 10; ++l) {
                        v = {i, j, k, l};
                        if(x == solve(s, v)) ans++;
                    }
                }
            }
        }
        cout << x << " " << ans << "\n";
    }

    return 0;
}

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

string s;
vector<int> p(4);

bool isLetter(char x){
    return 'a'<=x && x<='d';
}

int calc(char op,int lhs,int rhs){
    if(op=='+'){
        return lhs|rhs;
    }
    if(op=='*'){
        return lhs&rhs;
    }
    if(op=='^'){
        return lhs^rhs;
    }
    assert(false);
}
int Hash(int &pos){
    if(isLetter(s[pos])){
        return p[s[pos++]-'a'];
    }
    else{
        assert(s[pos++]=='[');
        char op=s[pos++];
        int lhs=Hash(pos);
        int rhs=Hash(pos);
        assert(s[pos++]==']');
        return calc(op,lhs,rhs);
    }
}
int main(){
    while(cin>>s,s!="."){
        string pbuf;
        cin>>pbuf;
        for(int i=0;i<4;i++){
            p[i]=pbuf[i]-'0';
        }
        int pos=0;
        int h=Hash(pos);
        int res=0;
        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++){
                        p[0]=a,p[1]=b,p[2]=c,p[3]=d;
                        pos=0;
                        if(h==Hash(pos)){
                            res++;
                        }
                    }
                }
            }
        }
        cout<<h<<" "<<res<<endl;
    }
    return 0;
}

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

int Hash(string S, string T) {
	stack<char> ST;
	stack<int> ST2;
	for (int i = 0; i < S.size(); i++) {
		if (S[i] == '[') continue;
		if (S[i] == ']') {
			char C = ST.top();
			ST.pop();
			int A, B;
			A = ST2.top();
			ST2.pop();
			B = ST2.top();
			ST2.pop();
			int D;
			if (C == '+') D = A | B;
			if (C == '*') D = A & B;
			if (C == '^') D = A ^ B;
			ST2.push(D);
		}
		else {
			if ('a' <= S[i] && S[i] <= 'd') {
				ST2.push(T[S[i] - 'a'] - '0');
			}
			else {
				ST.push(S[i]);
			}
		}
	}
	return ST2.top();
}

int main() {
	string S;
	while (cin >> S) {
		if (S == ".") return 0;
		string T;
		cin >> T;
		int ANS1 = Hash(S, T), ANS2 = 0;
		for (T[0] = '0'; T[0] <= '9'; T[0]++) {
			for (T[1] = '0'; T[1] <= '9'; T[1]++) {
				for (T[2] = '0'; T[2] <= '9'; T[2]++) {
					for (T[3] = '0'; T[3] <= '9'; T[3]++) {
						if (ANS1 == Hash(S, T)) ANS2++;
					}
				}
			}
		}
		cout << ANS1 << ' ' << ANS2 << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
template <typename T> using V = vector<T>;

int a, b, c, d;

void calc(const string &s, int &idx, stack<int> &st) {
    if(s[idx] == '[') {
        idx++;
        calc(s, idx, st);
        calc(s, idx, st);
        int l = st.top(); st.pop();
        int r = st.top(); st.pop();
        int res;
        switch(s[idx]) {
            case '+': res = l | r; break;
            case '*': res = l & r; break;
            case '^': res = l ^ r; break;
            default: assert(false);
        }
        st.push(res);
        idx++;
        assert(s[idx] == ']');
        idx++;
    } else {
        switch(s[idx]) {
            case 'a': st.push(a); break;
            case 'b': st.push(b); break;
            case 'c': st.push(c); break;
            case 'd': st.push(d); break;
            default: assert(false);
        }
        idx++;
    }
}

void set_abcd(int num) {
    d = num % 10; num /= 10;
    c = num % 10; num /= 10;
    b = num % 10; num /= 10;
    a = num % 10; num /= 10;
}

int calc_hash(const string &s, int num) {
    set_abcd(num);
    stack<int> st;
    int idx = 0;
    calc(s, idx, st);
    return st.top();
}

bool solve() {
    string s;
    cin >> s;
    if(s == ".") return false;
    reverse(s.begin(), s.end());
    for(char &c : s) {
        if(c == ']') c = '[';
        else if(c == '[') c = ']';
    }
    int num;
    cin >> num;
    int hash = calc_hash(s, num);
    int ans = 0;
    for(int i = 0; i < 10000; i++) {
        int hash_i = calc_hash(s, i);
        ans += (hash == hash_i);
    }
    cout << hash << ' ' << ans << endl;
    return true;
}

int main() {
    while(solve());
    return 0;
}