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++: #define  _CRT_SECURE_NO_WARNINGS
#pragma comment (linker, "/STACK:526000000")

#include "bits/stdc++.h"

using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL

#define MOD 998244353LL
#define seg_size 262144 * 4LL
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

unsigned long xor128() {
	static unsigned long x = 123456789, y = 362436069, z = 521288629, w = time(NULL);
	unsigned long t = (x ^ (x << 11));
	x = y; y = z; z = w;
	return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
void init() {
	iostream::sync_with_stdio(false);
	cout << fixed << setprecision(20);
}

#define int ll

int go[4] = {};

int find(State &a) {
	if (*a == '[') {
		a++;
		int b = 0;
		if (*a == '+') {
			b = 0;
		}
		else if (*a == '*') {
			b = 1;
		}
		else if (*a == '^') {
			b = 2;
		}
		a++;
		int c = find(a);
		int d = find(a);
		if (b == 0) {
			c |= d;
		}
		else if (b == 1) {
			c &= d;
		}
		else {
			c ^= d;
		}
		a++;
		return c;
	}
	else {
		int hoge = go[*a - 'a'];
		a++;
		return hoge;
	}
}

void solve(){
	while (true) {
		string a;
		cin >> a;
		if (a == ".") return;
		int ans[10000] = {};
		for (int i = 0; i < 10000; ++i) {
			int now = i;
			REP(q, 4) {
				go[3-q] = now % 10;
				now /= 10;
			}
			State b = a.begin();
			ans[i] = find(b);
		}
		int b;
		cin >> b;
		int cnt = 0;
		REP(q, 10000) {
			if (ans[b] == ans[q]) cnt++;
		}
		cout << ans[b] << " " << cnt << endl;
	}
}

#undef int

int main() {
	init();
	solve();
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>

using namespace std;
#define int long long int
const int INF = 1001001001001001LL;

int di[] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dj[] = {0, 1, 0, -1, 1, 1, -1, -1};

string s;

int f(char a, char b, char c, char d, int l, int r){
    //cout << l << " " << r << endl;
    if(r == l){

        if(s[l] == 'a') return a - '0';
        if(s[l] == 'b') return b - '0';
        if(s[l] == 'c') return c - '0';
        if(s[l] == 'd') return d - '0';
    }
    
    char op = s[l + 1];
    //cerr << op << endl;
    int x, y;
    l += 2;
    if(s[l] != '['){
        x = f(a, b, c, d, l, l);
        l++;
    }else{
        int nr = l;
        while(s[nr] != ']'){
            nr++;
        }
        x = f(a, b, c, d, l, nr);
        l = nr + 1;
    }

    
    if(s[l] != '['){
        y = f(a, b, c, d, l, l);
        l++;
    }else{
        int nr = l;
        while(s[nr] != ']'){
            nr++;
        }
        y = f(a, b, c, d, l, nr);
        l = nr + 1;
    }

    int ret;
    if(op == '+') ret = x | y;
    else if(op == '*') ret = x & y;
    else(op == '^') return x ^ y;
    

    //cerr << op << " " << x << " " << y << " " << ret << endl;
    return ret;

}

signed main(){
    
    //cin >> s;
    //cout << f('0', '4', '0', '4', 0, s.size()) << endl;
    
    while(1){
        cin >> s;
        if(s == ".") break;
        string p; cin >> p;
        int n = s.size();

        int val = f(p[0], p[1], p[2], p[3], 0, n - 1);
        int ans = 0;
        
        for(char a = '0'; a <= '9'; a++){
            for(char b = '0'; b <= '9'; b++){
                for(char c = '0'; c <= '9'; c++){
                    for(char d = '0'; d <= '9'; d++){
                        if(val == f(a, b, c, d, 0, n - 1)) ans++;
                    }
                }
            }
        }

        cout << val << " " << ans << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define pb push_back
const int N = 110000;
int pos;
char s[N];
map<char,int> mp;
int number(){
    return mp[s[pos++]];
}
int factor(){
    if(s[pos] == '['){
        pos++;
        int res = factor();
        pos++;
        return res;
    }
    else if(s[pos]=='+'){
        pos++;
        return factor()|factor();
    }
    else if(s[pos]=='*'){
        pos++;
        return factor()&factor();
    }
    else if(s[pos]=='^'){
        pos++;
        return factor()^factor();
    }
    else return number();
}
        
main(){
    while(1){
        scanf("%s",s);
        if(s[0]=='.')exit(0);
        map<int,int> cnt;
        int k,ans;cin>>k;
        rep(i,10000){
            int now = i;
            rep(j,4){
                mp['d'-j]=now%10;
                now/=10;
            }
            pos = 0;
            int t = factor();
            if(k==i){
                ans = t;
            }
            cnt[t]++;
        }
        cout << ans << " " << cnt[ans] << endl;
    }
}