M : Toy workshop

13Dec13

I recently participated in the Advent Programming Contest 2013 organised by the IEEE Student Branch Klagenfurt (with support from Alpen-Adria-Universität Klagenfurt), wherein – as I’d recently received from Udacity a certificate for Introduction to Programming : Problem Solving with Java (CS046) – I challenged myself to program my solutions to the problems posed by the contest using only Java (I’d used C and C++ when I’d participated in the contest in 2012); I solved eighteen of the problems posed by the contest, achieving a rank of 10th (out of 155 participants).

As submissions of solutions to the contest are now closed, I’m posting here the solutions I submitted to the contest.

Problem (M; medium difficulty):

Toy workshop

Advent Programming Contest 2013 - M

The elves maintain a huge workshop where they produce a variety of gifts out of different materials. Help the elves to keep track about their stock and production.

Problem

Write a program that first reads a list of raw materials in stock, consisting of an integer number indicating the amount followed by the material, materials separated by commas. The following lines contain the items to be produced and the materials they are made from, for example:

Treehouse: 50 wood, 100 nails

Your program shall keep a list of the available material stock. Each item line should be responded by an “(item name) produced.” until there is not enough material in stock to produce the item – your program should write an “Out of stuff!” message and terminate in this case. Items and materials names can have spaces within the name. Spaces before or after commas or colons should be ignored.

Example

Input

100 wood, 1000 nails, 20 paint, 100 PLA
Treehouse: 50 wood, 100 nails
Treehouse: 50 wood, 100 nails
Barbie: 5 PLA
Rocking horse: 10 wood, 2 paint

Output

Treehouse produced.
Treehouse produced.
Barbie produced.
Out of stuff!

Solution (M.java):

import java.util.ArrayList;
import java.util.Scanner;

class DigitFinder
{
  public static int digit( String s, int i )
  {
    for( ; i < s.length(); i ++ )
    {
      final char c = s.charAt( i );
      if( c >= '0' && c <= '9' )
        return i;
    }
    return -1;
  }
}

class Material
{
  public String m_name;
  public int m_quantity;

  public Material( String p_name, int p_quantity )
  {
    m_name = p_name;
    m_quantity = p_quantity;
  }
}

class Materials
{
  private ArrayList<Material> m_materials = new ArrayList<Material>();

  public Materials( String p )
  {
    int i = DigitFinder.digit( p, 0 );
    do
    {
      int k = p.indexOf( ' ', i );
      final int quantity = Integer.parseInt( p.substring( i, k ));
      i = k + 1;
      k = p.indexOf( ',', i );
      if( k == -1 )
        k = p.length();
      final String name = p.substring( i, k ).trim();
      m_materials.add( new Material( name, quantity ));
      i = DigitFinder.digit( p, k );
    }
    while( i != -1 );
  }

  public boolean subtractQuantity( String p_name, int p_quantity )
  {
    for( Material m : m_materials )
      if( m.m_name.equals( p_name ))
        if( m.m_quantity - p_quantity >= 0 )
        {
          m.m_quantity -= p_quantity;
          return true;
        }
        else
          return false;
    return false;
  }
}

public class M
{
  public static void main( String p[] )
  {
    Scanner s = new Scanner( System.in );
    Materials m = new Materials( s.nextLine());
    boolean out_of_stuff = false;
    do
    {
      String l = s.nextLine();
      int i = l.indexOf( ':' );
      String gift = l.substring( 0, i ).trim();
      i = DigitFinder.digit( l, i );
      do
      {
        int k = l.indexOf( ' ', i );
        final int quantity = Integer.parseInt( l.substring( i, k ));
        i = k + 1;
        k = l.indexOf( ',', i );
        if( k == -1 )
          k = l.length();
        final String name = l.substring( i, k ).trim();
        if( !m.subtractQuantity( name, quantity ))
          out_of_stuff = true;
        i = DigitFinder.digit( l, k );
      }
      while( !out_of_stuff && i != -1 );
      if( !out_of_stuff )
        System.out.println( gift + " produced." );
      else
        System.out.println( "Out of stuff!" );
    }
    while( !out_of_stuff );
  }
}

Testing:

100 wood, 1000 nails, 20 paint, 100 PLA
Treehouse: 50 wood, 100 nails
Treehouse produced.
Treehouse: 50 wood, 100 nails
Treehouse produced.
Barbie: 5 PLA
Barbie produced.
Rocking horse: 10 wood, 2 paint
Out of stuff!
Advertisements


No Responses Yet to “M : Toy workshop”

  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: