The all important t-shirt round, top 500 advance but 1000 t-shirts were on offer. Four problems for round 2, in the end advancing meant solving all the small inputs and the easiest large input, or one less small in a very fast time. Even with a slow time that was enough to get the t-shirt, 2 small and a large was okay if you were fast, or the second small was for the hardest problem. I think I stood a decent chance of advancing if I had of been competing this year, definitely would have gotten the t-shirt…

**Q1) Given a 2D grid where some cells propel you in a direction, what is the minimum number of cell with direction that need to be changed to ensure that regardless of starting location, you never fall off the edge? (Note its not always possible to solve, so return ‘impossible’ if there is no solution.)**

A1) I didn’t immediately get this problem, but when I looked at it again I realized it was trivial. Seems the contestants agreed, with very high success ratio and high solving rate.

In the end it boils down to the question, why would you fall off the edge at all? Because a cell is pointing there with nothing else in the way. So for each such cell, just point it at another cell with a direction. If you can’t then the problem is impossible and you should return as such. Otherwise you are done, just count how many cells you had to change. The run time of the simple brute force is obviously no more than O(WH*max(W, H)) which given the constraints is trivial. A tighter bound is actually O(WH) every cell can only be traversed at most 5 times, once to find the cells with direction, and then 4 times from being reached from each direction by other cells. So even if the problem had of allowed much larger grids, it would still have performed in time.

**Q2) Determine the minimal time to fill a container of volume V to temperature X, given a bunch of sources of flow rate F_i and temperature T_i.**

A2) The small input here has only 2 flow sources, and the problem gives the formula for mixing temperatures. Solve simple simultaneous equations in 2 variables and you are done. The inputs are given to 4 decimal places, and the answer is 1 part in 10^6 accuracy, so doubles sound like they would be fine here. But this isn’t quite like other such floating point problems. This one has the ‘can’t be done’ answer, if both temperatures are the same side of the actual temperature. So you need to be sure that you don’t introduce a numerical error which means that in the case where one of the input temperatures is equal to the desired temperature, you incorrectly return impossible. One solution is to ignore the fact that the question specifies things as 4dp, and just multiply everything by 10000. These factors of 10000 cancel out in determining the time, so you can switch to mostly integer arithmetic, just converting back to floating point for the final division. You have to be careful with overflows though.

The large input is too large to brute force, which suggests a greedy approach. My best guess was to order them by temperature. I took a look at some passing solutions and one of them did exactly this. Order by temperature, then slowly prune either the hottest or coldest until you switch from the output being too hot to being too cold or vice versa. Then its like solving the small input, just with one high flow mixed temperature of everything, and the last temperature you removed. I found it interesting that they didn’t bother to solve the simultaneous equations though, they just binary searched for the flow rate that produced the right output temperature. (Since you can simulate lower flow rates just be having that tap turned off for part of the time.)

Another solution I saw instead binary searched over ‘can it be solved by time x’. They also sorted by temperature, but instead of pruning backwards one by one, they built outwards from the middle. Given the time t, they had actual volumes rather than rates, and could determine if each volume from a given tap could be mixed with some from another to create the right temperature. As they used up each tap, they moved to the next one out. Eventually all of the colder than target taps or all of the hotter than target taps get used up, and the the rest is discarded. If the total used volume is greater than or equal to target, success and binary search a smaller time, otherwise a longer time is needed.

**Q3) Given a set of sentences which are in ‘English’ or ‘French’ determine the minimum number of words that must be in common between English and French. The first two sentences are labelled, the rest are unknown. **

A3) The small input looks like a trivial brute force, but unless you start by replacing strings with numbers, the hash/equality cost is quite high and will probably stop your code from running in time. So relabel everything to numbers. Given the constraints there will be no more than a few hundred distinct words which you can number consecutively 0 to N, so rather than hash lookups you can just used indexed arrays. Enumerate the 2^18 scenarios adding to the counts of times you’ve seen each ‘word’ against either English or French arrays. Then sweep them both looking for anything which has both positive for the same index.

The large input however had me stumped. My best guess was some kind of maximal matching or flow graph, but I couldn’t see how to construct it. Looking at other solutions it appears max flow graph was the answer.

Each sentence has two nodes, each distinct word has two nodes. Unlimited flow from the first to second nodes for each sentence, but only unit flow between the first and second nodes for each distinct word. Then connect the second node of each sentence to the first node of each distinct word in the sentence, and vice versa – both with unlimited flow. The answer is then the maximum flow from the first node of the first sentence, to the second node of the second sentence.

I don’t understand why this works though, maximum flow for a minimal answer. Even the meaning of each node doesn’t seem obvious. The two nodes per sentence seems redundant since there is only unlimited flows involved, probably just a convenience rather than having source and sink nodes for the first two sentences especially. A flow between the two nodes for a word means they must be both English and French.

Looking at the case of just the input two sentences, every word in common allows a flow which goes first node of first sentence, second node of first sentence, first node of word, second node of word, first node of second sentence, second node of second sentence. Words not in common don’t allow flows because they just form a cycle with their parent sentence. Next simplest example, one word third sentence which is in common with English (or French) but not both. It gets ignored. Good. Three word sentence with two in common with English and one in common with French. One more flow, from first sentence through the fact its in common with English, to third sentence, then over to French by the one in common with that. The second potential flow gets stuck at the 3rd sentence. It all works, but the why still escapes me.

**Q4) Given a cylinder labelled with a rectangular grid of positive numbers such that each number has exactly that many neighbours of the same value determine how many such labelling exists, modulo 1 billion and 7. Two labellings are considered distinct only if they aren’t rotations of each other.**

A4) Brute force on the small input here seems scary, but back tracking algorithm stands a good chance of running in time, because the problem is so restrictive. Some smarts might still be needed. Pure brute force is 3^36, which is obviously too slow. (4 and higher can’t be on the board, 5+ trivially, 4 because it implies an infinite board.)

Looking more deeply unlocks this problem. There are limited number of ‘units’ which a solution can be made up of. A row of 2’s where nothing above or below is a 2. 2 row’s of 3 where nothing above or below is a 3. These 2 are the obvious ones given in the question itself. The trick is to come to understand there are only 3 more building blocks. They all involve just 1’s and 2’s.

122 122 112222 222112 1222 1212 2212

Each can only be placed next to itself in a row, and each can be rotated a number of places equal to its width, but again rotated versions can only be next to rotated versions.

The problem then becomes a DP over rows. 2 rows of 3s separating one of 4 scenarios of 2’s. One which uses one row, 2 which use 2 rows, but have 3 or 6 rotation variants, one which uses 3 rows with 4 variants. The trick is to make sure you don’t over count. Each variant is like a rotation, so if you only use one pair of rows of mixed 1 and 2, you have actually over counted by exactly 3 or 6 times. One solution is to assume that you are always going to over count by a factor of 12, so divide the result by a factor of 12. Then at the deepest level of your recursion, force over counting to be 12. If you’ve not used any 1 or 2 mixed, return 12, if you’ve used only the first kind, return 4, only the second kind (or second and first), return 2, only the 3rd kind return 3, both the 1st and 3rd or 2nd and 3rd (or all 3) return 1. The DP is then on rows and 4 bools representing whether you’ve ever used each of the 3 types of 1,2 mixed and whether the last set of rows were all 3’s or not.