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)
}

Python: def parse_hash(s, pointer):
  head = s[pointer]
  if head == "[":
    pointer += 1
    op, pointer = parse_op(s, pointer)
    h1, pointer = parse_hash(s, pointer)
    h2, pointer = parse_hash(s, pointer)
    return calc(op, h1, h2), pointer + 1
  else:
    l, pointer = parse_letter(s, pointer)
    return l, pointer
 
def parse_op(s, pointer):
  return s[pointer], pointer + 1

def parse_letter(s, pointer):
  return int(s[pointer]), pointer + 1

def calc(op, h1, h2):
  if op == "+":
    return h1 | h2
  if op == "*":
    return h1 & h2
  if op == "^":
    return h1 ^ h2

from collections import defaultdict
from itertools import product
while True:
  s = input()
  if s == ".":break
  p = input()
  score = defaultdict(int)
  nums = [chr(i) for i in range(ord("0"), ord("9") + 1)]
  for a, b, c, d in product(nums, repeat=4):
    temp = s.replace("a", a)
    temp = temp.replace("b", b)
    temp = temp.replace("c", c)
    temp = temp.replace("d", d)
    score[parse_hash(temp, 0)[0]] += 1
  
  for i in range(4):
    s = s.replace(chr(ord("a") + i), p[i])
  hs = parse_hash(s, 0)
  print(hs[0], score[hs[0]])

Python: import sys,functools
if sys.version_info[0]>=3:
	maketrans=str.maketrans
else:
	from string import maketrans

O=lambda a:lambda b:a|b
A=lambda a:lambda b:a&b
X=lambda a:lambda b:a^b
while True:
	s=sys.stdin.readline().rstrip().translate(maketrans('+*^[]','OAX()'))
	if s=='.': break
	for c in 'abcd': s=s.replace(c,'(%s)'%c)
	s=s[1:-1]
	n=int(sys.stdin.readline())
	r=eval(s.translate(maketrans('abcd','%04d'%n)))
	print('%d %d'%(r,sum(r==eval(s.translate(maketrans('abcd','%04d'%i))) for i in range(10000))))

Python: # AOJ 2883: Proof of Knowledge
# Python3 2018.7.11 bal4u

def calc(a, b, c, d, i):
    global S
    if S[i] == '[':
        op = S[i+1]
        if S[i+2] == '[': v1, i = calc(a, b, c, d, i+2)
        else:
            if S[i+2] == 'a': v1 = a
            elif S[i+2] == 'b': v1 = b
            elif S[i+2] == 'c': v1 = c
            else: v1 = d
            i += 3

        if S[i] == '[': v2, i = calc(a, b, c, d, i)
        else:
            if S[i] == 'a': v2 = a
            elif S[i] == 'b': v2 = b
            elif S[i] == 'c': v2 = c
            else: v2 = d
            i += 2
        if op == '+': ans = v1 | v2
        elif op == '*': ans = v1 & v2
        else: ans = v1 ^ v2
    else:
        if S[i] == 'a': ans = a
        elif S[i] == 'b': ans = b
        elif S[i] == 'c': ans = c
        else: ans = d
        i += 1
    return [ans, i]

while True:
    S = input()
    if S == '.': break
    P = input()
    a, b, c, d = map(int, list(P))
    val, cnt = calc(a, b, c, d, 0)[0], 0
    for a in range(0, 10):
        for b in range(0, 10):
            for c in range(0, 10):
                for d in range(0, 10):
                    if calc(a, b, c, d, 0)[0] == val: cnt += 1
    print(val, cnt)

Python: def solve(s,p):
    if s[0]!='[':
        return[p//1000,p//100%10,p//10%10,p%10][ord(s[0])-ord('a')]
    i=3
    cs=s[2]=='['
    ce=0
    while cs!=ce:
        if s[i]=='[':
            cs+=1
        elif s[i]==']':
            ce+=1
        i+=1
    j=i+1
    cs=s[i]=='['
    ce=0
    while cs!=ce:
        if s[j]=='[':
            cs+=1
        elif s[j]==']':
            ce+=1
        j+=1
    l=solve(s[2:i],p)
    r=solve(s[i:j],p)
    if s[1]=='+':
        return l|r
    elif s[1]=='*':
        return l&r
    else:
        return l^r
while True:
    s=input()
    if s=='.':break
    p=int(input())
    a=solve(s,p)
    c=0
    for i in range(10000):
        if solve(s,i)==a:
            c+=1
    print(a,c)

Python: # AOJ 2883: Proof of Knowledge
# Python3 2018.7.11 bal4u

def calc(a, b, c, d, i):
    global S
    if S[i] == '[':
        op = S[i+1]
        if S[i+2] == '[': v1, i = calc(a, b, c, d, i+2)
        else:
            if S[i+2] == 'a': v1 = a
            elif S[i+2] == 'b': v1 = b
            elif S[i+2] == 'c': v1 = c
            else: v1 = d
            i += 3

        if S[i] == '[': v2, i = calc(a, b, c, d, i)
        else:
            if S[i] == 'a': v2 = a
            elif S[i] == 'b': v2 = b
            elif S[i] == 'c': v2 = c
            else: v2 = d
            i += 2
        if op == '+': ans = v1 | v2
        elif op == '*': ans = v1 & v2
        else: ans = v1 ^ v2
    else:
        if S[i] == 'a': ans = a
        elif S[i] == 'b': ans = b
        elif S[i] == 'c': ans = c
        else: ans = d
        i += 1
    while i < len(S) and S[i] == ']': i += 1
    return [ans, i]

while True:
    S = input()
    if S == '.': break
    P = input()
    a, b, c, d = map(int, list(P))
    val, cnt = calc(a, b, c, d, 0)[0], 0
    for a in range(0, 10):
        for b in range(0, 10):
            for c in range(0, 10):
                for d in range(0, 10):
                    if calc(a, b, c, d, 0)[0] == val: cnt += 1
    print(val, cnt)