Contest Fail

Last weekend I had a few hours to spare, so I decided to work on one of the problems from CodeSprint2, InterviewStreet’s contest to find the best programming talent.

Since I didn’t have long to work, I decided to try to complete the algorithm problem with the highest value. I chose to work on Count Strings (closed now). Given a regular expression, and a string length N, count the number of distinct strings of length N which the regular expression can match.

I looked at the sample test case, spent some time thinking about the problem and decided to try it. Although bounds for test cases were provided, I gave them only a cursory glance. This would turn out to be a mistake.

In my daily work, I typically focus on getting things working rather than spending time on determining the optimal way to do things. It is with this mindset that I decided that I would actually enumerate every string that a regular expression could produce, and then count them. I failed even before I began.

The regular expression language was limited to the alphabet {a, b}, with operators star, union, and concatenation.

I decided to use Haskell, which has the terseness of Ruby or Python, but the performance and type checking of a compiled language. I created a data type to represent the regular expressions. Recursive data types made this representation very straightforward.

data RegularExpression = Symbol Char
  | Concat RegularExpression RegularExpression
  | Union RegularExpression RegularExpression
  | Star RegularExpression
  deriving (Show, Eq)

I had the enumeration part complete fairly quickly. Pattern matching makes everything really intuitive and readable. The star operation required the most code.

listPossibilities (Symbol c) limit
  | limit > 0 = [c]
  | otherwise = []

listPossibilities (Concat r1 r2) limit = combos where
  o1 = listPossibilities r1 limit
  o2 = listPossibilities r2 limit
  combos = [ a ++ b | a <- o1, b <- o2, length (a++b) <= limit ]

listPossibilities (Union r1 r2) limit = possibilities where
  o1 = listPossibilities r1 limit
  o2 = listPossibilities r2 limit
  possibilities = o1 ++ o2

listPossibilities (Star _) 0 = [""]
listPossibilities (Star r1) limit = possibilities where
  opt = listPossibilities r1 limit
  possibilities = "": whileLimit opt limit optSet
  optSet = Set.fromList opt 

  -- | Uses nub right now, really inefficient. probably should use some sort of memoization
  whileLimit :: [String] -> Int -> [String] -> [String]
  whileLimit base lim acc = pos where
    new = List.nub [a++b | a <- base, b <- acc, length (a++b) <= lim]
    pos = if null new || length new < length acc then acc
          else whileLimit base lim (List.nub $ acc ++ new)

The next component I needed was a parser to convert input into these regular expression objects. I used the Parsec parser module to construct the parser. This part actually took a bit longer, as I tried to refine the parser to accept deeply nested expressions.

The regular expression union parser below parses a parenthesized expression or symbol, followed by a pipe, and another parenthesized expression or symbol, returning a RegularExpression object.

reUnion :: GenParser Char st RegularExpression
reUnion = do
  re1 <- pexpr <|> reSymbol
  char '|'
  re2 <- pexpr <|> reSymbol
  return (Union re1 re2)

Once I got the parser working, I glued it together with the enumeration portion, and submitted it to the contest. It immediately failed to compiled due to a dependency on Test.HUnit I had in my code. I reorganized the code so that I could easily remove this dependency, and resubmitted.

This time, it took, but when I went back after a few minutes to check the submission status, it had failed all but 1 of the test cases. It had taken too much time! I went back to the submission guidelines and noted that there was a time limit of 5 minutes.

Originally, I had used an algorithm which enumerated by checking distinctness using a List type. I knew at the time that this was very inefficient, but I focused on finishing the implementation at the time. I went back to this part and used a Set instead. I was optimistic that this would fix my problems and I would be able to knockout at least another test case.

I resubmitted and I failed 10 tests again. Then, I reread the problem, and looks at the boundaries for N. N could be up to 100,000 (IIRC)! Of course enumeration (especially the way I was doing it which would enumerate many strings of less than length N) would fail–I had taken a totally incorrect approach to this problem. I did not need to enumerate–I only needed to give a number of potential strings. This could’ve been done without enumeration. For example, given the expression a*, and a number N, there is only 1 possibility. Given (a|b)^*, 2^N. For example, one of the test cases was (a|b)^*, N=5, which has 32 possibilities. Another of the expressions was a^*ba^*. Since b can only appear once, the possible strings include strings of  (N-1) a’s and a single b. There are N such combinations (the number of positions b can be in, in an N length string): baa…, aba…, aab…, … . This is the approach I should’ve taken.

I left the solution there, and acknowledged my defeat. Source here: https://github.com/jamiely/count-strings

Cocos2d iOS Game: Balloon Burst

My employer gives us a week off for the holidays, so I’d been trying to decide what to do with all of the time. Although I originally didn’t want to work on any programming projects, I wound up working on a few. One of them was inspired by a play of the iOS game Train Yard. I was paying particular attention to the loading screen when I noticed it had been created using Cocos2d. I’d seen articles about the engine before, so I decided to take another look at the Website. I was curious about what other game engines were in popular use. Besides Unity, I didn’t notice any that were more popular than Cocos2d. I decided to investigate further and found a tutorial on the site.

Before I knew it, I was working through the tutorial. The API was simple, straight-forward, and intuitive, and I was soon done. Still, I wanted to use it for my own project. Unfortunately, the holidays were winding down, and Spring term classes will start soon, so time is very limited.

As a prepubescent child, I played a shareware game where balloons would rise on the screen. You’d use your mouse to draw a bowstring, flying an arrow horizontally through the balloon field, timing the ascent of a balloon. It’s this game that immediately came to mind when I tried to think of simple games. The satisfaction of that balloon popping noise made me long to play. (A satisfaction only outpaced by the sound of popping bubble-wrap.)

So, I decided to work on a game like that other game, and started on making balloons float up. I went to http://openclipart.org and found a great balloon-popping sound at http://soundbible.com. I started out by using CCActions, a CCSequence composed of CCMoveUp and CCCallFuncND to perform cleanup. The balloons would randomly spawn at a certain interval below the screen, and rise to a height above the screen, out of the viewport, and be removed from the layer.

Somewhere along the line, I decided that an bow and arrow would be unnecessary, and that touching a balloon would be enough to pop it. At the same time, I’d target the game at children. I implemented procedures to detect a balloon touch, show a particle effect upon touch, play the popping noise, and remove the balloon from the layer immediately.

Play-testing confirmed this mechanic was a little boring in of itself. I figured that popping balloons should release things stored inside, so you should be able to earn points for popping balloons and touching a “treasure item” dropped from the balloon, things like: diamonds, coins, and treasure boxes. I also added clouds in the background, a timer, and score display, the concept of rounds, and a game over state.

I got this working, but it didn’t seem especially targeted towards children. Lisa, my wife, tried it and confirmed as much. We talked about marking each balloon with a letter, and using the balloons to spell words or elsewise learn about words.

After thinking for a bit, I decided that balloons marked with a letter would release a word starting with that letter. When you touched the word dropped by a balloon, that word would be spoken aloud. I downloaded Audacity, and had Lisa recite a list of words. I spliced them and loaded them into my project Resources folder. Once I got the code working, so that the word sound would play when the word was touched, the audio seemed to low, like it had not been normalized. Even after reducing the volume of the background music and popping sound, the words weren’t loud enough. We’ll have to redo the word sounds at some point.

At this point, I’m mostly done, but may add some more graphics and clean up the code. This project has been a great introduction to Cocos2D and Objective-C programming, in general.

Check out game footage below and the complete source code, including assets, on Github: http://github.com/jamiely/ios-balloon-burst