// Phil Dexheimer
// 7/23/02
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* This solution represents each LED as a bit, and represents
   combinations of LEDs as a bitfield.  Since there are only
   7, a char works fine.*/

/* These variables are just in place to make it easier to
   describe each LED digit */
const char a = 1 << 0;
const char b = 1 << 1;
const char c = 1 << 2;
const char d = 1 << 3;
const char e = 1 << 4;
const char f = 1 << 5;
const char g = 1 << 6;

const char digitsOn[] = {a | b | c | e | f | g,
                         c | f,
                         a | c | d | e | g,
                         a | c | d | f | g,
                         b | c | d | f,
                         a | b | d | f | g,
                         a | b | d | e | f | g,
                         a | c | f,
                         a | b | c | d | e | f | g,
                         a | b | c | d | f | g};
                          
void main ()
{
 FILE *In = fopen("digit.in", "rt");
 char line[50], broken, on;
 int potential[10];
 int i;
 while (fgets(line, sizeof(line), In))
  {
   //'broken' and 'on' are bitfields which represent the input case
   broken = 0;
   for (i=0;(line[i] != '\n') && line[i];i++)
     broken |= 1 << (line[i]-'a');
   on = 0;
   fgets(line, sizeof(line), In);
   for (i=0;(line[i] != '\n') && line[i];i++)
     on |= 1 << (line[i]-'a');
   
   //'potential' will store the answer when we're finished. This initializes it
   //to contain all zeros
   memset(potential, 0, sizeof(potential));
   for (i=0;i<10;i++)
    {
     //The digit we're considering can only be an answer if all of the LEDs
     //which are currently lit are lit when this digit is shown
     potential[i] = ((on & digitsOn[i]) == on);
     if (potential[i])
      {
       //This takes into account the broken LEDs
       //Let's break the expression down:
       //(1) broken & digitsOn[i] -- The LEDs that would normally be on, but are broken
       //(2) on | (1) -- The LEDs that are on, combined with the broken ones that should be on
       //iff (2) is equal to digitsOn[i], then this is one of our answers
       if ((on | (broken & digitsOn[i])) != digitsOn[i])
         potential[i] = 0;
      }
    }
     
   for (i=0;i<10;i++)
     if (potential[i])
       printf("%5d", i);
   printf("\n");
  } 
 fclose(In);
}

