title: (Un)folding a sheet of paper according to a pattern and giving the order of the layers
body: <p>Last Friday, in a French programming contest, we were given a paper folding problem which was as follows:</p>
  <blockquote>
  <p>Given a square sheet of paper and a folding pattern, write a function &quot;fold(pattern)&quot; that will give the order of the layers that will result from the cumulative folding (in half) of the sheet in respect of the pattern.&quot;</p>
  </blockquote>
  <p>That may not be very clear, so let me explain a bit (having a square sheet of paper handy might help to understand, if you're willing to go that far to help me out).</p>
  <p>Suppose that we have a square sheet of paper which we draw a N*N grid on and then number all its inner squares. Given a pattern &quot;LTRB&quot;, we would fold this paper vertically in half and put the resulting left part on to the right part. Then, we would fold it horizontally and put the top part onto the bottom part. Then, we would fold it vertically again and put the right part onto the left part. Finally, we would fold it horizontally and put the bottom part on the top part. This leaves us with a sheet of paper that is the size of one square and N^2 layers. The expected answer is the resulting order of each original square.</p>
  <p>For example, if we have a draw a 2*2 grid on a square sheet of paper and then number each inner square from 1 to 4 (top left to bottom right, horizontally), and given the pattern &quot;LT&quot;, we would have this happening:</p>
  <pre><code>fold(&quot;LT&quot;):
  
   1 | 2   L  1,2  T
  ---|--- ==&gt; --- ==&gt; 2,1,3,4
   3 | 4      3,4
  </code></pre>
  <p>and with a &quot;RB&quot; pattern, for example:</p>
  <pre><code>fold(&quot;RB&quot;): 
  
   1 | 2   R  2,1  B
  ---|--- ==&gt; --- ==&gt; 3,4,2,1
   3 | 4      4,3
  </code></pre>
  <p>As you would have probably guessed, it basically boils down to a recursive transformation of an N*N matrix. This was the easy part, and here is my solution:</p>
  <p><a href="http://ideone.com/QaXGq" rel="nofollow noreferrer">http://ideone.com/QaXGq</a></p>
  <p>Now comes the interesting part.</p>
  <ul>
  <li>Write a function unfold(order) that will, for a given resulting layer
  ordering, give the pattern that produced this ordering. Note that
  unfold(fold(&quot;LRTB&quot;)) =&gt; &quot;LRTB&quot;</li>
  </ul>
  <p>And then my brain stopped working for a while and I didn't have the time (2 hours left from 4 hours total) to come up with a fast enough solution.</p>
  <p>My initial ideas were:</p>
  <ol>
  <li><p>Try to brute force it. Because we know the length of the input is N^2, we can create an initial matrix and try all possible folding until we reach the same order as the input. O(4^N) complexity, not viable.</p>
  </li>
  <li><p>Brute force reverse. Start from the input, and try all unfolding possibilities until we reach a correct initial state. A little bit better, but still too slow.</p>
  </li>
  <li><p>???</p>
  </li>
  </ol>
  <p>Anyone has an idea?</p>