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: import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    def f(s):
        t = [int(c) for c in S()]
        a = [t[ord(c) - ord('a')] if c in 'abcd' else c for c in s]

        def ff(a):
            if len(a) == 1:
                return a[0]
            l = len(a)
            for i in range(l-4):
                if a[i] == '[' and a[i+4] == ']':
                    op = a[i+1]
                    b = a[i+2]
                    c = a[i+3]
                    t = -1
                    if op == '+':
                        t = b | c
                    elif op == '*':
                        t = b & c
                    elif op == '^':
                        t = b ^ c
                    return ff(a[:i] + [t] + a[i+5:])
            return -1

        r = ff(a)
        c = 0
        for k in range(10000):
            tt = [k//(10**i)%10 for i in range(4)]
            aa = [tt[ord(c) - ord('a')] if c in 'abcd' else c for c in s]
            kk = ff(aa)
            if kk == r:
                c += 1
        return '{} {}'.format(r,c)

    while 1:
        n = S()
        if n == '.':
            break
        rr.append(f(n))

    return '\n'.join(map(str, rr))


print(main())

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
    while s.count('[',2,i)!=s.count(']',2,i):
        i+=1
    j=i+1
    while s.count('[',i,j)!=s.count(']',i,j):
        j+=1
    l=solve(s[2:i])
    r=solve(s[i:j])
    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: 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
    while s.count('[',2,i)!=s.count(']',2,i):
        i+=1
    j=i+1
    while s.count('[',i,j)!=s.count(']',i,j):
        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: 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: import sys
input = sys.stdin.readline

def hash(a, b, c, d, f, start, end):
    i = start + 1
    while i < end:
        if f[i] in "+^*":
            i += 1
            if f[i] == "a":
                tmp1 = a
            elif f[i] == "b":
                tmp1 = b
            elif f[i] == "c":
                tmp1 = c
            elif f[i] == "d":
                tmp1 = d
            elif f[i] == "[":
                i, tmp1 = hash(a,b,c,d,f,i,end)
            i += 1
            if f[i] == "a":
                tmp2 = a
            elif f[i] == "b":
                tmp2 = b
            elif f[i] == "c":
                tmp2 = c
            elif f[i] == "d":
                tmp2 = d
            elif f[i] == "[":
                i, tmp2 = hash(a,b,c,d,f,i,end)
            if f[start+1] == "+":
                return i+1, tmp1 | tmp2
            elif f[start+1] == "^":
                return i+1, tmp1 ^ tmp2
            elif f[start+1] == "*":
                return i+1, tmp1 & tmp2


def main():
    while True:
        s = input().strip()
        if s[0] == ".":
            break
        p = input().strip()
        a = int(p[0])
        b = int(p[1])
        c = int(p[2])
        d = int(p[3])
        if s[0] in "abcd":
            if s[0] == 'a':
                print(a, 1000)
            elif s[0] == 'b':
                print(b, 1000)
            elif s[0] == 'c':
                print(c, 1000)
            elif s[0] == 'd':
                print(d, 1000)
            #print(int(p['s[0]' - 'a']), 1)
            continue
        _,ans1 = hash(a,b,c,d,s,0,len(s))
        count = 0
        for a in range(10):
            for b in range(10):
                for c in range(10):
                    for d in range(10):
                        _, x = hash(a,b,c,d,s,0,len(s))
                        if x == ans1:
                            count += 1
        print(ans1, count)

if __name__ == "__main__":
    main()