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 <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;
const long double PI = acos((long double)(-1));

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;

int f(int a, int b, int c, int d, string s) {
	vector<string> st;
	for (int i = 0; i < s.size(); i++) {
		if (s[i] == 'a') s[i] = (char)('0' + a);
		if (s[i] == 'b') s[i] = (char)('0' + b);
		if (s[i] == 'c') s[i] = (char)('0' + c);
		if (s[i] == 'd') s[i] = (char)('0' + d);
	}
	for (int i = 0; i < s.size(); i++) {
		if (s[i] == ']') {
			vector<string> t(3);
			int cur = 0;
			while (st.back() != "[") {
				t[cur] = st.back();
				st.pop_back();
				cur++;
			}
			st.pop_back();
			int res = 0;
			if (t[2] == "+") {
				res = stoi(t[0]) | stoi(t[1]);
			}
			if (t[2] == "*") {
				res = stoi(t[0]) & stoi(t[1]);
			}
			if (t[2] == "^") {
				res = stoi(t[0]) ^ stoi(t[1]);
			}
			st.emplace_back(to_string(res));
		}
		else {
			st.emplace_back(string(1, s[i]));
		}
	}
	return stoi(st[0]);
}

void solve() {
	string s; cin >> s; if (s == ".") exit(0);
	int p; cin >> p;
	map<int, int> mp;
	int res = 0;
	vector<int> cnt(20000);
	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++) {
		int t = a * 1000 + b * 100 + c * 10 + d;
		if (p == t) res = f(a, b, c, d, s);
		cnt[f(a, b, c, d, s)]++;
	}
	cout << res << " " << cnt[res] << "\n";
}

int main()
{

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	int kkt = 89; while (kkt) solve();
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i, s, e) for(int i = (int)(s); i < (int)(e); ++i)
#define rep(i, n) REP(i, 0, n)


string s, t;
using State = string::iterator;
int atom(State& it);
int term(State& it);
int form(State& it);

int atom(State& it) {
  auto c = *it;
  assert(c >= 'a' && c <='d');
  it++;
  return t[c - 'a'] - '0';
}
int term(State& it) {
  auto c = *it;
  ++it;
  if(c == '+') {
    return form(it) | form(it);
  }
  else if(c == '*') {
    return form(it) & form(it);
  }
  else if(c == '^') {
    return form(it) ^ form(it);
  }
  assert(false);
}
int form(State& it) {
  auto c = *it;
  if(c == '[') {
    ++it;
    int tmp = term(it);
    ++it;
    return tmp;
  }
  assert(c >= 'a' && c <= 'd');
  return atom(it);
}

string conv(int x) {
  string s = to_string(x);
  if(s.size() < 4) {
    s = string(4 - s.size(), '0') + s;
  }
  return s;
}

int main() {
  while(cin >> s) {
    if(s == ".") break;
    cin >> t;
    auto it = s.begin();
    int x = form(it);
    int cnt = 0;
    rep(i, 10000) {
      it = s.begin();
      t = conv(i);
      int y = form(it);
      if(x == y) cnt++;
    }
    cout << x << " " << cnt << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-9;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

int f(int &id,string s,string t){
	if(isalpha(s[id])) return t[s[id++]-'a']-'0';
	id++;
	char c=s[id];
//	cout<<c<<' '<<id<<endl;
	id++;
	int x=f(id,s,t),y=f(id,s,t);
	id++;
	if(c=='+') return x|y;
	if(c=='*') return x&y;
	if(c=='^') return x^y;
	assert(0);
}

int main(){
	while(1){
		string s,t;
		cin>>s;
		if(s==".") break;
		cin>>t;
		int id=0,x=f(id,s,t),res=0;
		for(int i=0;i<10000;i++){
			t=to_string(i);
			while(t.size()<4) t='0'+t;
			id=0;
			if(x==f(id,s,t)) res++;
		}
		cout<<x<<' '<<res<<endl;
	}
}