#!/usr/bin/env python3 import sys # Map abcdefg -> 1234567 (orig positions) def mapit(preface): M = {} def mset(sig, val): if val in M: print(val) print(M) raise Exception("val already in M!") else: M[val] = set(sig) todo = preface[:] while todo: sig = todo.pop(0) if len(sig) == 2: mset(sig, "1") continue elif len(sig) == 3: mset(sig, "7") continue elif len(sig) == 4: mset(sig, "4") continue elif len(sig) == 7: mset(sig, "8") continue elif len(sig) == 6: # either 0/6/9 if contains(sig, M, "4"): mset(sig, "9") continue elif "4" in M and contains(sig, M, "7"): mset(sig, "0") continue elif "4" in M and "7" in M: mset(sig, "6") continue elif len(sig) == 5: # either 2/3/5 if contains(sig, M, "1"): mset(sig, "3") continue if "1" in M and ("6" in M or "9" in M): if contained(sig, M, "6") or contained(sig, M, "9"): mset(sig, "5") continue else: mset(sig, "2") continue # unfound, retry todo.append(sig) return M def contains(sig, M, query): if query not in M: return False for c in M[query]: if c not in sig: return False return True def contained(sig, M, query): if query not in M: return False for c in sig: if c not in M[query]: return False return True total = 0 for l in sys.stdin: # Sort into two buckets per line seen = False preface = [] output = [] for sig in l.strip().split(" "): if sig == "|": seen = True continue if seen: output.append(sig) else: preface.append(sig) # Now get a mapping... M = mapit(preface) out = "" for o in output: s = set(o) for m, v in M.items(): if s == v: out += m total += int(out) print(total)