Download All Files (as .zip)

ECOO 2013 Local - Problem #1: Take a Number

The problem:

Due to overwhelming demand, the principal has installed one of those "take a number" dispensers to help the attendance secretary manage the line for late slips. The dispenser is filled with slips of paper numbered in order from 1 to 999. The principal has made sure to order lots of refills! The attendance desk opens at 8:00 am every morning and closes at 3:00 pm. When a late student arrives they take the next number from the machine, and when the attendance secretary is ready, he calls the next number in order. When a student takes the last number, the secretary immediately refills the machine with a new set of numbers from 1 to 999. At 3:00 pm, he removes the dispenser and stores it for the next day, then serves any students who are still waiting with numbers in their hands before closing for the day.

DATA11.txt (DATA12.txt for the second try) will contain detailed data for a number of days in the late slip lineup. The first line of the file contains an integer N (0 < N < 1000) representing the next number in the take a number machine. This will be followed by some number of lines (up to 1 000 000) representing the activity at the attendance desk. If a line contains the word "TAKE", it means a student has arrived and taken the next number (when a student takes the last number available, the machine is immediately refilled.) If a line contains the word "SERVE" it means that the attendance secretary has served the next student in line (this word will only appear in the file when there is at least one student waiting). If a line contains the word "CLOSE" it means that the desk has closed for the day and the attendance secretary will serve the students remaining in line and then go home. The very last line of the file will contain the string "EOF". At no time will there be more than 999 students waiting in line to be served.

Your job is to keep track of the line. Each time you encounter the word "CLOSE", you must print three integers on a single line, each separated by a single space. The first integer represents the number of students who were late that day, the second integer represents the number of students who remained in line after the desk was closed, and the third integer represents the next number in the take a number machine for the next day.

Sample Input

23 TAKE TAKE
TAKE TAKE TAKE
TAKE SERVE TAKE
SERVE CLOSE TAKE
TAKE TAKE TAKE
SERVE SERVE SERVE
SERVE TAKE CLOSE
CLOSE SERVE EOF
TAKE TAKE

Sample Output
3 0 26
3 2 29
8 5 37

My solution (in Java):

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

public class Problem_1_Take_A_Number {

    public static void main(String[] args) {

        try {

            ArrayList<String> list = new ArrayList<String>();

            // read in the file to the ArrayList
            Scanner file = new Scanner(new File("C:\\Users\\Mike\\Desktop\\DATA10.txt"));
            while (file.hasNextLine()) {
                list.add(file.nextLine());
            }

            int n = Integer.parseInt(list.get(0)); // initial next ticket number
            list.remove(0); // remove first number from list
            list.remove(list.size() - 1); // remove "EOF" from list

            int takes = 0, serves = 0;

            for (String s : list) {

                if (s.trim().equalsIgnoreCase("CLOSE")) {

                    System.out.print( String.valueOf(takes) + " "); // people late
                    System.out.print( String.valueOf(takes - serves) + " "); // people not served

                    // old solution for accounting for overflow. doesn't work because each overflow of 1000 causes this to be 1 more number off (1000 goes to 1, not 0)
                    // this solution for overflow is now in the else clause below
                    /*n += takes;
                    if (n > 999) { // have to refill the dispenser
                        n = 1 + (n % 1000); // get the overflow tickets
                    }*/

                    System.out.println( String.valueOf(n) ); // next available ticket number

                    takes = serves = 0;
                } else {

                    if (s.trim().equalsIgnoreCase("TAKE")) {
                        takes++;
                        n++;
                        // account for overflow
                        if (n > 999) { // have to refill the dispenser
                            n = 1 + (n - 1000); // get the overflow tickets
                        }
                    } else if (s.trim().equalsIgnoreCase("SERVE")) {
                        serves++;
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception");
        }
        
    }
}
DOWNLOAD as .java

My test cases (as .txt files):

Using their sample input:

23
TAKE
TAKE
SERVE
TAKE
SERVE
SERVE
CLOSE
TAKE
TAKE
TAKE
SERVE
CLOSE
TAKE
SERVE
TAKE
SERVE
TAKE
TAKE
TAKE
TAKE
TAKE
TAKE
SERVE
CLOSE
EOF

And the output to that is:

3 0 26
3 2 29
8 5 37

DOWNLOAD as .txt

Testing the 1000 ticket overflow:

990
TAKE
TAKE
SERVE
TAKE
SERVE
SERVE
CLOSE
TAKE
TAKE
TAKE
SERVE
CLOSE
TAKE
SERVE
TAKE
SERVE
TAKE
TAKE
TAKE
TAKE
TAKE
TAKE
SERVE
CLOSE
EOF

And the output to that is:

3 0 993
3 2 996
8 5 5

DOWNLOAD as .txt

Both of those test cases had exactly the expected output

Using their first judging test input (look at the download link), the expected output is:

1724 42 834
1110 0 945
1454 166 401
2263 135 666
282 26 948
497 85 446
0 0 446
1110 90 557
1626 93 185
342 3 527
DOWNLOAD as .txt

And that is exactly the expected output.

Using their second judging test input (look at the download link), the expected output is:

908 54 646
5 1 651
588 38 240
449 28 689
324 36 14
2170 143 186
2117 162 305
1620 75 926
1339 138 267
874 17 142
1458 111 601
1080 67 682
2041 127 725
1898 107 625
555 43 181
1907 85 90
1977 127 69
2091 138 162
1232 49 395
1873 151 270
DOWNLOAD as .txt

And that is exactly the expected output.


           Created: March 26, 2014
     Last updated: June 15, 2014
Completed in full by: Michael Yaworski