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++: /*  -*- coding: utf-8 -*-
 *
 * 2883.cc: Proof of Knowledge
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int N = 4;
const char opcs[] = "abcd+*^";

/* typedef */

enum { OP_A, OP_B, OP_C, OP_D, OP_OR, OP_AND, OP_XOR };

struct Node {
  int op;
  Node *lpt, *rpt;
  Node() {}

  int calc(int ps[]) {
    if (op >= OP_A && op <= OP_D) return ps[op];
    int c0 = lpt->calc(ps), c1 = rpt->calc(ps), r;
    switch (op) {
    case OP_OR:  r = (c0 | c1); break;
    case OP_AND: r = (c0 & c1); break;
    case OP_XOR: r = (c0 ^ c1); break;
    }
    return r;
  }

  void print(int *ps = NULL) {
    if (op >= OP_A && op <= OP_D) {
      if (ps != NULL) printf("%d", ps[op]);
      else putchar('a' + op);
    }
    else {
      putchar('(');
      lpt->print(ps);
      putchar(opcs[op]);
      rpt->print(ps);
      putchar(')');
    }
  }
};

/* global variables */

char cmap[256];
char se[84], sp[6];
int ps[N], qs[N];
Node buf[80], *bpt;

/* subroutines */

Node *parse(char *(&cpt)) {
  Node *npt = bpt++;
  char c = *(cpt++);
  if (c == '[') {
    npt->op = cmap[*(cpt++)];
    npt->lpt = parse(cpt);
    npt->rpt = parse(cpt);
    cpt++; // ']'
  }
  else
    npt->op = cmap[c], npt->lpt = npt->rpt = NULL;
  return npt;
}

/* main */

int main() {
  for (int i = 0; opcs[i]; i++) cmap[opcs[i]] = i;

  while (scanf("%s", se) == 1 && se[0] != '.') {
    scanf("%s", sp);
    for (int i = 0; i < N; i++) qs[i] = sp[i] - '0';

    bpt = buf;
    char *cpt = se;
    Node *exp = parse(cpt);
    //exp->print(); putchar('\n');

    int qh = exp->calc(qs);
    //exp->print(qs); printf("=%d\n", qh);

    memset(ps, 0, sizeof(ps));
    int cnt = 0;

    for (;;) {
      int ph = exp->calc(ps);
      if (ph == qh) cnt++;

      int k = 0;
      while (k < N) {
	if (++ps[k] < 10) break;
	ps[k++] = 0;
      }
      if (k >= N) break;
    }

    printf("%d %d\n", qh, cnt);
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
 
#define State string::const_iterator
class ParseError {};
 
int Hash(State &begin);
int OP(State &begin);
int Letter(State &begin);
int factor(State &begin);
void consume(State &begin, char expected);
 
int a, b, c, d;
 
// 四則演算の式をパースして、その評価結果を返す。
int Hash(State &begin) {
    int ret = factor(begin);
 
    return ret;
 
}
 
// 乗算除算の式をパースして、その評価結果を返す。
int OP(State &begin) {
    int ret = 0;
    if(*begin == '*'){
        consume(begin, '*');
        int left_num = Hash(begin);
        int right_num = Hash(begin);
        ret = left_num & right_num;
    }
    else if(*begin == '+'){
        consume(begin, '+');
        int left_num = Hash(begin);
        int right_num = Hash(begin);
        ret = left_num | right_num;
    }
    else if(*begin == '^'){
        consume(begin, '^');
        int left_num = Hash(begin);
        int right_num = Hash(begin);
        ret = left_num ^ right_num;
    }
    else{
        cout << " out of range" << *begin << endl;
        ret = 0;
        int dummy = 5/ret;
    }
 
    return ret;
}
 
// 数字の列をパースして、その数を返す。
int Letter(State &begin) {
    if(*begin=='a'){
        consume(begin, 'a');
        return a;
    }
    else if(*begin=='b'){
        consume(begin, 'b');
        return b;
    }
    else if(*begin=='c'){
        consume(begin, 'c');
        return c;
    }
    else if(*begin=='d'){
        consume(begin, 'd');
        return d;
    }
    else{
        consume(begin, 'o');
        return -20;
    }
}
 

int factor(State &begin) {
    if (*begin == '[') {
        consume(begin, '['); // '['を飛ばす。
        int ret = OP(begin);
        consume(begin, ']'); // ']'を飛ばす。
        return ret;
    }
    else {
        return Letter(begin);
    }
}  

 
// beginがexpectedを指していたらbeginを一つ進める。
void consume(State &begin, char expected) {
    if (*begin == expected) {
        begin++;
    } else {
        cerr << "Expected '" << expected << "' but got '" << *begin << "'"
             << endl;
        cerr << "Rest string is '";
        while (*begin) {
            cerr << *begin++;
        }
        cerr << "'" << endl;
        throw ParseError();
    }
}
 
int main() {
  
    while(true){
 
        string S;
 
        cin >> S;
 
        if(S[0] == '.'){
            break;
        }
 
        string P;
        cin >> P;

        int count = 0;
        map<int,int> hash_memory;
       
        for(int i=0; i<10000; i++){

            State s = S.begin();

            a = i/1000;
            b = (i/100)%10;
            c = (i/10)%10;
            d = i%10;
            s = S.begin();
            hash_memory[Hash(s)]++;
        }

        State s = S.begin();

        stringstream ss;

        ss << P[0];
        ss >> a;
        ss.clear();

        ss << P[1];
        ss >> b;
        ss.clear();

        ss << P[2];
        ss >> c;
        ss.clear();

        ss << P[3];
        ss >> d;
        ss.clear();

        int P_Hash = Hash(s);

        cout << P_Hash << " " << hash_memory[P_Hash] << endl;
        
 
    }
 
    return 0;
}