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 <string>
using namespace std;
int myhas(string s, int ptr, int a, int b, int c, int d){
  if(s[ptr] == 'a'){ return a; }
  else if(s[ptr] == 'b'){ return b; }
  else if(s[ptr] == 'c'){ return c; }
  else if(s[ptr] == 'd'){ return d; }
  else if(s[ptr] == '['){
    int p = 0;
    if(s[ptr+2] == '['){
      int cnt = 1;
      while(cnt != 0){
	p++;
	if(s[ptr+2+p] == ']'){cnt--;}
	else if(s[ptr+2+p] == '['){cnt++;}
      }
    }
    // cout << "p = " << p << endl;
    // cout << "X now gaze " << "s[" << ptr+2 << "] == " << s[ptr+2] << endl;
    int X = myhas(s, ptr+2, a, b, c, d);
    // cout << "Y now gaze " << "s[" << ptr+2+p+1 << "] == " << s[ptr+2+p+1] << endl;
    int Y = myhas(s, ptr+2+p+1, a, b, c, d);
    if(s[ptr+1] == '+'){
      return (X | Y) & 15;
    }else if(s[ptr+1] == '*'){
      return (X & Y) & 15;
    }else if(s[ptr+1] == '^'){
      return (X ^ Y) & 15;
    }
  }
  // cout << "something wrong" << endl;
  return 0;
}

int main(void){
  for(;;){
    string S;
    cin >> S;
    if(S == "."){return 0;}
    string P;
    cin >> P;
    int mypassword = stoi(P);
    int md = mypassword % 10;
    int mc = (mypassword / 10) % 10;
    int mb = (mypassword / 100) % 10;
    int ma = (mypassword / 1000);
    int ans = myhas(S,0,ma,mb,mc,md);
    int cnt = 0;
    for(int a = 0 ; a <= 9 ; a++){
      for(int b = 0 ; b <= 9 ; b++){
	for(int c = 0 ; c <= 9 ; c++){
	  for(int d = 0 ; d <= 9 ; d++){
	    if(ans == myhas(S,0,a,b,c,d)){
	      cnt++;
	    }
	  }
	}	
      }
      }
    cout << ans << " " << cnt << endl;
  }
}

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

template<typename T>
struct Parser {
	using itr = string::const_iterator;
	itr now;
	T ans;
	map<char, int> abcd;
	const struct ex {
		vector<char> abcd;
		ex() {
			abcd.resize(4);
			for(int i = 0; i < 4; ++i) {
				abcd[i] = (char)('a' + i);
			}
		}
	} ex;
	Parser(const string &s, map<char, T> &abcd) : abcd(abcd) {
		now = s.begin();
		ans = Hash(now);
	}
	T Hash(itr &now) {
		if(*now == '[') {
			next(now, '[');
			char op = *now;
			T ret;
			if(op == '+') {
				next(now, '+');
				T hash1 = Hash(now);
				T hash2 = Hash(now);
				ret = hash1 | hash2;
			}
			if(op == '*') {
				next(now, '*');
				T hash1 = Hash(now);
				T hash2 = Hash(now);
				ret = hash1 & hash2;
			}
			if(op == '^') {
				next(now, '^');
				T hash1 = Hash(now);
				T hash2 = Hash(now);
				ret = hash1 ^ hash2;
			}
			next(now, ']');
			return ret;
		} else {
			return Letter(now);
		}
	}
	T Letter(itr &now) {
		T ret = abcd[*now];
		next(now, ex.abcd);
		return ret;
	}
	void next(itr &now, const char expected) {
		vector<char> req = {expected};
		next(now, req);
	}
	void next(itr &now, const vector<char> &expected) {
		for(char c : expected) {
			if(*now == c) {
				now++;
				return;
			}
		}
		cerr << "now " << *now << "\n";
		for(char c : expected) cerr << c << "\n";
	}
};

int main() {
	string s;
	while(cin >> s and s != ".") {
		map<char, int> abcd;
		map<int, int> cnt;
		int p; cin >> p;
		int hash;
		for(int i = 0; i < 10000; ++i) {
			int tmp = i;
			for(char c = 'd'; c >= 'a'; --c) {
				abcd[c] = tmp % 10;
				tmp /= 10;
			}
			Parser<int> parse(s, abcd);
			cnt[parse.ans]++;
			if(i == p) hash = parse.ans;
		}
		cout << hash << " " << cnt[hash] << "\n";
	}
	return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
#define rep(i, n) for (i = 0; i < n; ++i)

int a[4];
int expr(string &s, int &p) {
    if (isalpha(s[p])) return a[s[p++] - 'a'];
    assert(s[p++] == '[');
    char op = s[p++];
    int x = expr(s, p);
    int y = expr(s, p);
    assert(s[p++] == ']');
    if (op == '+') return x | y;
    if (op == '*') return x & y;
    if (op == '^') return x ^ y;
}

int main() {
    string s;
    while (cin >> s, s != ".") {
        string pass; cin >> pass;
        for (int i = 0; i < 4; i++) a[i] = pass[i] - '0';
        int p = 0, ans = expr(s, p);
        int cnt = 0;
        rep(a[0], 10) rep(a[1], 10) rep(a[2], 10) rep(a[3], 10) {
            int p = 0;
            if (ans == expr(s, p)) cnt++;
        }
        cout << ans << " " << cnt << endl;
    }
    return 0;
}