Google Code Jam 2012 started this weekend and this years qualification round was simpler than other years.

## Problem 1 – Speaking in Tongues

The first exercise is a character mapping problem. You will get some gibberish input and have to encode it to normal english sentences.

gibberish: ejp mysljylc kd kxveddknmc re jsicpdrysi ready: our language is impossible to understand

And there was only the small data set (with many tries to solve it) available. Ok, looks like a classical problem and I start thinking to build a solution with counting common english characters or iterate over all permutation to find words (from an english dictionary).

I was wondering after reading the three-line-example! You can manually fill out the complete mapping table and you have no hassle with permuting over the missing characters! The python solution has the mapping table and use the build-in function translate.

import string, sys input = file(sys.argv[1]).readline trans = string.maketrans("ynficwlbkuomxsevzpdrjgthaq", "abcdefghijklmnopqrstuvwxyz") for case in range(int(input())): print "Case #%d: %s" % (case+1, input().strip().translate(trans))

## Problem 2 – Dancing With the Googlers

The second problem was interesting – here the problem description: You’re watching a show where Googlers (employees of Google) dance, and then each dancer is given a triplet of scores by three judges. Each triplet of scores consists of three integer scores from 0 to 10 inclusive. The judges have very similar standards, so it’ssurprising if a triplet of scores contains two scores that are 2 apart. No triplet of scores contains scores that are more than 2 part.

For example: (8, 8, 8) and (7, 8, 7) are not surprising. (6, 7, 8) and (6, 8, 8) are surprising. (7, 6, 9) will never happen. The total points for a Googler is the sum of the three scores in that Googler’s triplet of scores. The best result for a Googler is the maximum of the three scores in that Googler’s triplet of scores. Given the total points for each Googler, as well as the number of surprising triplets of scores, what is the maximum number of Googlers that could have had a best result of at least p?

How to identify the highest values of a “not surprising” and “surprising” triple? Simply remember the MODULO operator.

def maxNotSurprising(n): b = n / 3 #1: 0+0+1 #2: 0+1+1 if n%3>0: return b+1 #0+0+0 return b def maxSurprising(n): b = n / 3 #2: 0+0+2 if n%3==2: return b+2 #1: 0+1+0 if n%3==1: return b+1 #3: 0+1+2 if n%3==0 and n>0: return b+1 #0: 0+0+0 return b

And the result script has to check if there are surprising triples left and count the p-hits.

import sys input = file(sys.argv[1]) def solve(s, p, ti): c = 0 for t in ti: maxNot = maxNotSurprising(t) maxSur = maxSurprising(t) if s>0 and maxSur>maxNot and maxSur>=p and maxNot= p: c+=1 s-=1 else: if maxNot>=p: c+=1 return c for case in range(int(input.readline())): values = [int(x) for x in input.readline().split()] print "Case #%d: %d" % (case+1, solve(values[1], values[2], values[3:]))

## Problem 3: Recycled Numbers

The third exercise is a number crunching problem and looks like my perfect number search article.

Problem description: A pair of distinct positive integers (n, m) is **recycled** if you can obtain m by moving some digits from the back of n to the front without changing their order. For example, (12345, 34512) is a recycled pair since you can obtain 34512 by moving 345 from the end of 12345 to the front. Note that n and m must have the same number of digits in order to be a recycled pair. Neither n nor m can have leading zeros.

First we need to generate all rotated variants (and no doubles). I calculated it as integers – the solution by rotating character on a string is possible too (but slower). My fault on the large data set was calculation the digits with the logarithm function … I got a float rounding error.

int(math.log(1000,10))

was not 3!

def digits(n): #return int(math.log(n,10)) d=0 while n>9: n/=10 d+=1 return d def variants(n): d = digits(n) f = 10**d v = set([n]) for x in range(d): n = f*(n%10)+n/10 v.add(n) return v

I had no better idea than iterating over all numbers and checking the condition for every variant.

Condition: Given integers A and B with the same number of digits and no leading zeros, how many distinct recycled pairs (n, m) are there with A ? n < m ? B?

def check(a, b): c = 0 for n in range(a, b): for m in variants(n): if m > n and m

This is a non-perfect solution and does run 6 minutes on the large data set (on my old PC)! Puh – 8 minutes with downloading and uploading was a brief timespan.

## Problem 4: Hall of Mirrors

The suggested time of two hours was over after solving three problems – so I skipped the hardest and last problem.

Give some comments on pages/articles who solved this problem or other/better solutions for the first three problems.

- http://vexorian.blogspot.de
- python solution for small set
- and a java solution found on github – agiamas
- google offers a contest analysis for all problems

And check the language statistics for the qualification round 2012!

## Other problems solved

Check other our articles about other google code jam problems:

- Space Emergency is a nice little puzzle from Google Code Jam 2011
- Picking Up Chicks from Round 1B 2010
- Minimum scalar product from Round 1A 2008
- Decoding alien numbers from the Practice Problems
- a very short solution for alien language from the 2009 qualification round
- The rotate problem from the Round 1A 2010 from nico and my self
- Theme Park from the Qualification Round 2010
- the Snapper Chain from Qualification Round 2010

#### Christian Harms

#### Latest posts by Christian Harms (see all)

- google code jam 2013 – tic-tac-toe-Tomek solution - April 16, 2013
- Google code jam 2013 – the lawnmower - April 14, 2013
- code puzzles and permutations - April 11, 2013