X : The Twelve Days of Christmas

24Dec12

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 (X; easy difficulty):

The Twelve Days of Christmas

“The Twelve Days of Christmas” is a Christmas carol that cumulatively enumerates a series of gifts given on each of the twelve days of Christmas. The lyrics for the first verse are:

On the first day of Christmas, my true love sent to me:
A Partridge in a pear-tree

adding another gift in each verse until the last verse:

On the twelfth day of Christmas, my true love sent to me:
12 Bells ringing
11 Ladies spinning
10 Ships a sailing
9 Lords a leaping
8 Ladies dancing
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree

There are many variations of this song in which the objects are in a different order or different kind of gifts are described.

Problem

Implement a program that generates the lyrics based on a list of gift names. The program shall read the gift names in 12 lines from standard input, whereas the first line is the gift from the first day of Christmas. The gift names are given without a numeral, so the “12”, “11”, … “3”, “2”, “a” need to be added by your program. Note that the “A” is uppercase in the first verse, but lowercase and preceded by “and” in the subsequent ones. Your program shall print all twelve verses without empty lines between them. Each line shall end with exactly one newline. After printing the lyrics for the given input, your program shall terminate.

Example

Input

Partridge in a pear-tree
Turtle doves
French hens
Canary birds
Gold rings
Geese a laying
Swans a swimming
Ladies dancing
Lords a leaping
Ships a sailing
Ladies spinning
Bells ringing

Output

On the first day of Christmas, my true love sent to me:
A Partridge in a pear-tree
On the second day of Christmas, my true love sent to me:
2 Turtle doves
and a Partridge in a pear-tree
On the third day of Christmas, my true love sent to me:
3 French hens
2 Turtle doves
and a Partridge in a pear-tree
On the fourth day of Christmas, my true love sent to me:
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree
On the fifth day of Christmas, my true love sent to me:
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree
On the sixth day of Christmas, my true love sent to me:
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree
On the seventh day of Christmas, my true love sent to me:
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree
On the eighth day of Christmas, my true love sent to me:
8 Ladies dancing
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree
On the ninth day of Christmas, my true love sent to me:
9 Lords a leaping
8 Ladies dancing
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree
On the tenth day of Christmas, my true love sent to me:
10 Ships a sailing
9 Lords a leaping
8 Ladies dancing
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree
On the eleventh day of Christmas, my true love sent to me:
11 Ladies spinning
10 Ships a sailing
9 Lords a leaping
8 Ladies dancing
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree
On the twelfth day of Christmas, my true love sent to me:
12 Bells ringing
11 Ladies spinning
10 Ships a sailing
9 Lords a leaping
8 Ladies dancing
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree

Solution (X.c):

#include <stdio.h>
#include <stdlib.h>

typedef struct t_CharStackNode_struct
{
  struct t_CharStackNode_struct *m_down;
  char m_char;
}
t_CharStackNode;

static t_CharStackNode *g_charStack_top = NULL;
static size_t g_charStack_length = 0;

void p_CharStack_Reset( void );
void p_CharStack_Input( FILE *p );
char *f_CharStack_String( void );

int main( void )
{
  int i = 0;
  char *giftName[ 12 ];
  for( ; i < 12; i ++ )
  {
    p_CharStack_Input( stdin );
    giftName[ i ] = f_CharStack_String();
  }
  char *day[ 12 ];
  day[ 0 ] = "first";
  day[ 1 ] = "second";
  day[ 2 ] = "third";
  day[ 3 ] = "fourth";
  day[ 4 ] = "fifth";
  day[ 5 ] = "sixth";
  day[ 6 ] = "seventh";
  day[ 7 ] = "eighth";
  day[ 8 ] = "ninth";
  day[ 9 ] = "tenth";
  day[ 10 ] = "eleventh";
  day[ 11 ] = "twelfth";
  for( i = 0; i < 12; i ++ )
  {
    printf( "On the %s day of Christmas, my true love sent to me:\n", day[ i ] );
    int k = i;
    for( ; k > 0; k -- )
      printf( "%d %s\n", k + 1, giftName[ k ] );
    if( i > 0 )
      printf( "and a" );
    else
      printf( "A" );
    printf( " %s\n", giftName[ 0 ] );
  }
  return 0;
}

static void p_CharStack_Delete( t_CharStackNode *p )
{
  if( p != NULL )
  {
    p_CharStack_Delete( p->m_down );
    free( p );
  }
}

void p_CharStack_Reset( void )
{
  p_CharStack_Delete( g_charStack_top );
  g_charStack_top = NULL;
  g_charStack_length = 0;
}

void p_CharStack_Input( FILE *p )
{
  char c;
  t_CharStackNode *n;
  p_CharStack_Reset();
  do
  {
    fscanf( p, "%c", &c );
    if( c != '\n' )
    {
      n = ( t_CharStackNode* )malloc( sizeof( t_CharStackNode ));
      n->m_down = g_charStack_top;
      g_charStack_top = n;
      n->m_char = c;
      if( g_charStack_length < ( size_t )-2 )
        g_charStack_length ++;
    }
  }
  while( c != '\n' );
}

char *f_CharStack_String( void )
{
  size_t i = 0, k = g_charStack_length - 1;
  t_CharStackNode *n = g_charStack_top;
  char *s = ( char* )malloc( sizeof( char ) * ( g_charStack_length + 1 ));
  for( ; i < g_charStack_length; i ++, k --, n = n->m_down )
    s[ k ] = n->m_char;
  s[ i ] = '\0';
  return s;
}

Testing:

gcc -std=c99 -Wall -lm X.c
mv a.out X
./X
On the first day of Christmas, my true love sent to me:
A Partridge in a pear-tree
On the second day of Christmas, my true love sent to me:
2 Turtle doves
and a Partridge in a pear-tree
On the third day of Christmas, my true love sent to me:
3 French hens
2 Turtle doves
and a Partridge in a pear-tree
On the fourth day of Christmas, my true love sent to me:
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree
On the fifth day of Christmas, my true love sent to me:
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree
On the sixth day of Christmas, my true love sent to me:
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree
On the seventh day of Christmas, my true love sent to me:
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree
On the eighth day of Christmas, my true love sent to me:
8 Ladies dancing
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree
On the ninth day of Christmas, my true love sent to me:
9 Lords a leaping
8 Ladies dancing
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree
On the tenth day of Christmas, my true love sent to me:
10 Ships a sailing
9 Lords a leaping
8 Ladies dancing
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree
On the eleventh day of Christmas, my true love sent to me:
11 Ladies spinning
10 Ships a sailing
9 Lords a leaping
8 Ladies dancing
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree
On the twelfth day of Christmas, my true love sent to me:
12 Bells ringing
11 Ladies spinning
10 Ships a sailing
9 Lords a leaping
8 Ladies dancing
7 Swans a swimming
6 Geese a laying
5 Gold rings
4 Canary birds
3 French hens
2 Turtle doves
and a Partridge in a pear-tree
Advertisements


No Responses Yet to “X : The Twelve Days of Christmas”

  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: