Training for Google Code Jam 2008 with ActionScript3

Imagine this, the contest day has come, you’re in front of your computer waiting the last 60 seconds for the contest to begin, it’s your first programming contest but you haven’t had time to try to answer the Practice Problems so you have no idea on how to solve even the easiest problem.

If you don’t have much time to practice or you just want to see a different solution to the practice problems, you have come to right place!!!.

Here, I’ll publish my own explained solutions to the practice problem you find in the GCJ Dashboard, some of this might be based on other, but the good think is that I’m not only publishing the source code, I’ll explain how the solutions work.

Some of the solutions are not mine, I found some in the google-codejam group (http://groups.google.com/group/google-code), here you will find them translated to ActionScript3; I’ll make sure to mention it when it is the case.

Why is ActionScript3 used in the solutions? Well, main reason is that this blog is about Flex, another good reason is that ActionScript3 will be very familiar if you use languages like Java, C++ or C#, and you can see it running embedded in this web page.

At this point you may wonder why am I publishing the solutions, why?

When I was at school I participated in many Math contests, something I learned those days is that sharing what you know is part of the fun on the competition, and it doesn’t hurt, by the contrary it makes a healthier competition because the skills and knowledge of everyone get improved.

In the next posts, I’ll be publishing the solution for the problems of the GCJ Dashboard, if you are like me you’ll like to try to solve them yourself before reading the solutions, if so give it a try at http://code.google.com/codejam/contest.

Now it’s time to get in shape with our programming skills =) good look and have fun!!!

Solution template

Before begin solving problem, we will make our Solution Template project, after we finish this we just have to focus on the solution logic.

First, we need an input field to read the small and large input data from the text files, the easiest way in this case is that you copy the content from the input files manually to a TextArea in your solution program. Why? because with Flex the other way is to upload the file to your web server, then tell your flex application where the file is and finally start reading, too many steps ;).

Then we need a textarea for input, mmm textarea for output and a button to process input and get the output :P.

Another thing we always have to do is to transform text input in a string[][] array, so here we have something the "Process" button have to do.

To make this general we will pass the string[][] input data to a function that will implement the solution logic. We will call this function TransformInput for all problems. Here is some code:

// Handles Process button click
private function handleProcess(event:MouseEvent):void{
	// Split input data into an string array
	var inputLines:Array = inputText.text.split("\r");
	var inputData:Array = [];
	for(var index:int; index < inputLines.length; index++){
		 inputData.push((inputLines[index] as String).split(" "));
	}
	outputText.text = TransformInput(inputData).join("\r"); 
}

// This functions implements the logic of your solution
private function TransformInput(inputData:Array):Array{
	var outputData:Array = []; // Output lines
	// This time we are dealing with a difficult chalenge
	// Question: Number of rows in the input
	outputData.push(inputData.length);
	return outputData;
}

We are done here, you can download the source code for this template and make your own changes.

AttachmentSize
SolutionTemplate.zip14.52 KB