#!/usr/bin/perl

$in_a_raw = "hostageranaredknight";
$in_b_raw = "phonedugupthediamond";
$in_c_raw = "fmrmeuhdndnacdasr#et";
$in_d_raw = '#wu$ubm#$hnxmubgit)u';
$in_e_raw = "ejnrhbmmoda^ct^ari(o";
$in_f_raw = "electricianwasajoker";

@map = (
 'e', '11111',
 't', '11110',
 'a', '11101',
 'o', '11100',
 'i', '11011',
 'n', '11010',
 's', '11001',
 'r', '11000',
 'h', '10111',
 'l', '10110',
 'd', '10101',
 'c', '10100',
 'u', '10011',
 'm', '10010',
 'f', '10001',
 'p', '10000',
 'g', '01111',
 'w', '01110',
 'b', '01100',
 'k', '01010',
 'j', '01000',

 'v', '01011',
 'x', '01001',

 '#', '01101',
 ')', '00111',
 '(', '00110',
 '$', '00011',
 '^', '00000',

 '1', '00001',
 '2', '00010',

 '4', '00100',
 '5', '00101',
 
 # unknown = qyz
);

%map = @map;
%reversemap = reverse @map;

@monster = ();

@crap = split('', $in_c_raw);
my @bits_c_raw = ();
foreach (@crap) {
  push @bits_c_raw, (split ('', $map{$_}));
}
@crap = split('', $in_f_raw);
my @bits_f_raw = ();
foreach (@crap) {
  push @bits_f_raw, (split ('', $map{$_}));
}
@crap = split('', $in_a_raw);
my @bits_a_raw = ();
foreach (@crap) {
  push @bits_a_raw, (split ('', $map{$_}));
}
@crap = split('', $in_b_raw);
my @bits_b_raw = ();
foreach (@crap) {
  push @bits_b_raw, (split ('', $map{$_}));
}
@crap = split('', $in_d_raw);
my @bits_d_raw = ();
foreach (@crap) {
  push @bits_d_raw, (split ('', $map{$_}));
}
@crap = split('', $in_e_raw);
my @bits_e_raw = ();
foreach (@crap) {
  push @bits_e_raw, (split ('', $map{$_}));
}

$length = 100;

$cut_0 = 1;
$cut_1 = 0;
$cut_2 = 0;
$cut_3 = 0;
$cut_4 = 0;

$cut_5 = 0;
$cut_6 = 1;
$cut_7 = 0;
$cut_8 = 0;
$cut_9 = 0;

$default = 0;

sub circuit {
  my ($in_a,$in_b,$in_c,$in_d,$in_e,$in_f) = @_;
  $in_a = ($in_a eq '1');
  $in_b = ($in_b eq '1');
  $in_c = ($in_c eq '1');
  $in_d = ($in_d eq '1');
  $in_e = ($in_e eq '1');
  $in_f = ($in_f eq '1');

  my $upper_blue = ($cut_0 ? $default : (! $in_a));
#print "ub is ", $upper_blue, "\n";
  my $red = ($cut_3 ? $default : ($in_f && ($in_a || $upper_blue)));
#print "red is ", $red, "\n";
  my $yellow = ($cut_5 ? $default : $in_e);
#print "yellow is ", $yellow, "\n";
  my $grey = ($cut_9 ? $default : $yellow);
#print "gray is ", $gray, "\n";
  my $lower_blue = ($cut_6 ? $default : (! $grey));
#print "lower_blue is ", $lower_blue, "\n";
  my $cc = $in_b && $in_d;
#print "cc is ", $cc, "\n";
  my $dd = (! $in_b) && (! $in_d);
#print "dd is ", $dd, "\n";
  my $brown = ($cut_4 ? $default : ($cc || $dd));
#print "brown is ", $brown, "\n";
  my $r = $lower_blue;
#print "r is ", $r, "\n";
  #my $j = $red;
  my $j = $brown;
#print "j is ", $j, "\n";
  my $l = ! $j;
#print "l is ", $l, "\n";
  my $g = $in_c && $j;
#print "g is ", $g, "\n";
  my $h = ! $in_c && $l;
#print "h is ", $h, "\n";
  my $e = $g || $h;
#print "e is ", $e, "\n";
  my $f = $in_e || $r;
#print "f is ", $f, "\n";
  my $c = $e && $f;
#print "c is ", $c, "\n";
  my $d = ! $e && ! $f;
#print "d is ", $d, "\n";
  return ($c or $d);
}

$giantresultstring = '';

print "a is ", @bits_a_raw, "\n";
print "b is ", @bits_b_raw, "\n";
print "c is ", @bits_c_raw, "\n";
print "d is ", @bits_d_raw, "\n";
print "e is ", @bits_e_raw, "\n";
print "f is ", @bits_f_raw, "\n";

print "\n";

for (1..$length) {
  my $in_a = shift(@bits_a_raw);
  my $in_b = shift(@bits_b_raw);
  my $in_c = shift(@bits_c_raw);
  my $in_d = shift(@bits_d_raw);
  my $in_e = shift(@bits_e_raw);
  my $in_f = shift(@bits_f_raw);
  $result = circuit($in_a,$in_b,$in_c,$in_d,$in_e,$in_f);
  $giantresultstring .= ($result ? '1' : '0');
}

print "? is ", $giantresultstring, "\n";

while ($giantresultstring ne '') {
  $giantresultstring =~ s/^(.....)//;
  print $reversemap{$1};
}

print "\n";
