L : Secret Santa

12Dec12

I recently participated in the Advent Programming Contest organised by the IEEE Student Branch Klagenfurt (with support from Alpen-Adria-Universität Klagenfurt and Universität Passau), achieving a rank of 11th (out of 118 participants); as submissions of solutions to that contest are now closed, I’m posting here the solutions I submitted to that contest.

Problem (L; medium difficulty):

Secret Santa

Santa’s presents should be a surprise for the kids, so Santa felt a need for encrypting his communication with the elves. They agree to use the Caesar cipher, which is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. The alphabet is thought to wrap around, so A follows after Z. The encryption key defines the shift that is used for the cipher. For example, with a shift of 5, A would be replaced by F, K would become P, Z would become E, etc. The method is named after Julius Caesar, who used it in his private correspondence. Santa and the elves further agree on the following rules:

  • Only alphabetical characters are to be encrypted, all other characters stay the same.
  • The upper/lowercase of a character is preserved

If the encryption key (an integer between 0 and 25) is known, a text can be encrypted and decrypted. Santa used a different key for each elf. Unfortunately Santa lost the notes with the keys, so he is now in deep trouble. Luckily, he has still a number of encrypted messages from the elves in his inbox. He also remembers that all messages had his name mentioned in the text.

Problem

Write a program that helps Santa recovering the keys. Your program should read one encrypted message per line, determine the encryption key and print the key followed by a newline. If the program reads an unencrypted message, the program should terminate.

Example

Input

Ijfw Xfsyf! N bnqq mfaj ymj ytd lzsx wjfid gd sjcy Rtsifd.
Mn ftmr? H vhkk gzud sn bgdbj sghr vhsg Rzmsz.
Qqqqw! Ftqdq ue m Tansanxuz uz tqdq. Emzfm, tqxb!
Hi Santa! All future messages will be encrypted!

Output

5
25
12

Solution (L.c):

#include <ctype.h>
#include <stdio.h>
#include <string.h>

typedef enum
{
  false = 0,
  true
}
bool;

int main( void )
{
  char c;
  int key;
  size_t sLength;
  char s[ 6 ];
  s[ 5 ] = '\0';
  l_LOOP:
    key = -1;
    sLength = 0;
    do
    {
      scanf( "%c", &c );
      if( key == -1 )
      {
        if( isalpha( c ) != 0 )
        {
          if( sLength < 5 )
            s[ sLength ] = c;
          sLength ++;
        }
        else
        {
          if( sLength == 5 )
          {
            if( strcmp( s, "Santa" ) == 0 )
              key = 0;
            else
            {
              char s2[ 6 ];
              strcpy( s2, s );
              int i = 0;
              for( ; (( i < 25 ) && ( key == -1 )); i ++ )
              {
                int k = 0;
                for( ; k < 5; k ++ )
                {
                  char c2 = tolower( s2[ k ] );
                  const bool isUppercase = ( isupper( s2[ k ] ) != 0 );
                  c2 ++;
                  if(( c2 >= 'a' ) && ( c2 <= 'z' ))
                    ;
                  else
                    c2 = 'a';
                  s2[ k ] = ( isUppercase )?toupper( c2 ):c2;
                }
                if( strcmp( s2, "Santa" ) == 0 )
                {
                  key = s[ 0 ] - s2[ 0 ];
                  if( key < 0 )
                    key += 26;
                }
              }
            }
          }
          sLength = 0;
        }
      }
    }
    while( c != '\n' );
    if( key > 0 )
    {
      printf( "%d\n", key );
      goto l_LOOP;
    }
  return 0;
}

Testing:

gcc -std=c99 -Wall -lm L.c
mv a.out L
./L
Ijfw Xfsyf! N bnqq mfaj ymj ytd lzsx wjfid gd sjcy Rtsifd.
5
Mn ftmr? H vhkk gzud sn bgdbj sghr vhsg Rzmsz.
25
Qqqqw! Ftqdq ue m Tansanxuz uz tqdq. Emzfm, tqxb!
12
Hi Santa! All future messages will be encrypted!
Advertisements


No Responses Yet to “L : Secret Santa”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: