L : Secret Santa (‘bonus’)

12Dec12

The following is a ‘bonus’ program for decrypting the messages given in problem L of the Advent Programming Contest organised by the IEEE Student Branch Klagenfurt (with support from Alpen-Adria-Universität Klagenfurt and Universität Passau.)

Source code (L (bonus).pas):

program L_bonus( input, output );

function f_String2Integer( p_input : string; var p_output : integer ) : boolean;
{Returns true if the string was converted to an integer without error.}
var
  errorOccurred : boolean;
  multiplier, output, i : integer;
begin
  errorOccurred := length( p_input ) = 0;
  if not errorOccurred then
    errorOccurred := ( length( p_input ) = 1 ) and (( p_input[ 1 ] = '-' ) or ( p_input[ 1 ] = '+' ));
  if not errorOccurred then begin
    multiplier := 1;
    output := 0;
    for i := length( p_input ) downto 1 do
      begin
        if p_input[ i ] in [ '0'..'9' ] then
          output := output + (( ord( p_input[ i ] ) - ord( '0' )) * multiplier )
        else if ( p_input[ i ] = '-' ) and ( i = 1 ) then
          output := -output
        else if ( p_input[ i ] = '+' ) and ( i = 1 ) then
          output := output
        else
          errorOccurred := true;
        multiplier := multiplier * 10
      end;
    if not errorOccurred then
      p_output := output
  end;
  f_String2Integer := not errorOccurred
end;

procedure p_Beep;
begin
  write( chr( 7 ))
end;

function f_IsLetter( p : char ) : boolean;
begin
  f_IsLetter := p in [ 'a'..'z', 'A'..'Z' ]
end;

function f_IsLowercase( p : char ) : boolean;
begin
  f_IsLowercase := p in [ 'a'..'z' ]
end;

function f_IsUppercase( p : char ) : boolean;
begin
  f_IsUppercase := p in [ 'A'..'Z' ]
end;

function f_ToLowercase( p : char ) : char;
begin
  if f_IsUppercase( p ) then
    f_ToLowercase := chr(( ord( p ) - ord( 'A' )) + ord( 'a' ))
  else
    f_ToLowercase := p
end;

function f_ToUppercase( p : char ) : char;
begin
  if f_IsLowercase( p ) then
    f_ToUppercase := chr(( ord( p ) - ord( 'a' )) + ord( 'A' ))
  else
    f_ToUppercase := p
end;

var
  s : string;
  key : integer;
  errorOccurred : boolean;
  c : char;
  cI : integer;
begin
  writeln( '- This is a ''bonus'' program for decrypting the messages given in problem L of the Advent Programming Contest.' );
  writeln( '---' );
  repeat
    writeln;
    writeln( 'Please enter the decryption key (an integer in the range 1..25, or 0 to quit.)' );
    writeln;
    write( 'Your input: ' );
    readln( s );
    key := -1;
    errorOccurred := not f_String2Integer( s, key );
    if not errorOccurred then
      errorOccurred := ( key < 0 ) or ( key > 25 );
    if errorOccurred then begin
        p_Beep;
        writeln;
        writeln( 'Sorry, an error occurred: your input was not an integer in the range 0..25.' )
      end
    else
      if key <> 0 then begin
        writeln;
        writeln( 'Please enter the message to decrypt.' );
        writeln;
        write( 'Your input: ' );
        repeat
          read( c );
          if f_IsLetter( c ) then begin
              cI := ord( f_ToLowercase( c )) - key;
              if not (( cI >= ord( 'a' )) and ( cI <= ord( 'z' ))) then
                cI := cI + 26;
              if f_IsUppercase( c ) then
                c := f_ToUppercase( chr( cI ))
              else
                c := chr( cI );
              write( c )
            end
          else
            write( c )
        until eoln;
        readln;
        writeln
      end
  until key = 0;
  writeln;
  writeln( '---' );
  writeln( '- This program will now quit.' )
end.

Execution:

- This is a 'bonus' program for decrypting the messages given in problem L of the Advent Programming Contest.
---

Please enter the decryption key (an integer in the range 1..25, or 0 to quit.)

Your input: 5

Please enter the message to decrypt.

Your input: Ijfw Xfsyf! N bnqq mfaj ymj ytd lzsx wjfid gd sjcy Rtsifd.
Dear Santa! I will have the toy guns ready by next Monday.

Please enter the decryption key (an integer in the range 1..25, or 0 to quit.)

Your input: 25

Please enter the message to decrypt.

Your input: Mn ftmr? H vhkk gzud sn bgdbj sghr vhsg Rzmsz.
No guns? I will have to check this with Santa.

Please enter the decryption key (an integer in the range 1..25, or 0 to quit.)

Your input: 12

Please enter the message to decrypt.

Your input: Qqqqw! Ftqdq ue m Tansanxuz uz tqdq. Emzfm, tqxb!
Eeeek! There is a Hobgoblin in here. Santa, help!

Please enter the decryption key (an integer in the range 1..25, or 0 to quit.)

Your input: 0

---
- This program will now quit.
Advertisements


No Responses Yet to “L : Secret Santa (‘bonus’)”

  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: