# 
# Why I should comment decisions when I write them.  When I solved
# the problem, I used a hash for each question's answer.  Just before I
# posted, I switched to a list.  Now abra -> abracadabra will never be
# an question/answer.
# 
# Here is my original solution.
# 
#! perl -w

# This is a fairly straightforward solution.  I have a map from
# four-letter questions to a hash of answers.  Of course, only single
# answers are correct.


use strict;

# Open the output.

open QH, "> questions" or die "Cannot open questions: $!\n";
open AH, "> answers" or die "Cannot open answers: $!\n";

# The key of %map is the four-letter "question", while the value of
# %map is a hash whose key is "answer".  In this way, we can count the
# keys of an element to see if it's unique.

my %map;

while (<>)
{
  chomp;

  # I've decided that the key is case insensitive, but the word is
  # not.  Therefore, iela is not a question, since it has two answers,
  # Dixieland and dixieland.

  my @letters = split //, lc;
  while (scalar @letters >= 4)
  {
    my $key = join('', @letters[0 .. 3]);
    shift @letters;

    # Only want the key if it all letters.
    next unless $key =~ /[a-z]{4}/;

    $map{$key}->{$_} = 0;
  }
}

while (my ($key, $value) = each %map)
{
  my @keys = keys %{$value};
  if (@keys == 1)
  {
    print QH "$key\n";
    print AH "$keys[0]\n";
  }
}

# Close the output.

close QH or die "Cannot close questions: $!\n";
close AH or die "Cannot close answers: $!\n";

# end file 08.easy
