# Re: Grid Folding (#63)

**URL:** <https://www.ruby-forum.com/t/re-grid-folding-63/96448>\
**Category:** Ruby\
**Created:** [January 24, 2006, 6:29am UTC](https://www.ruby-forum.com/t/re-grid-folding-63/96448 "2006-01-24T06:29:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [January 24, 2006, 6:29am UTC](https://www.ruby-forum.com/t/re-grid-folding-63/96448/1 "2006-01-24T06:29:18Z")

</div>

My solution, performs all the extra credits.

> **[iiNet Members Webspace End of Life | iiHelp](https://help.iinet.net.au/iinet-members-webspace-end-life)**
>
> The iiNet Members Webspace service has reached End of Life.

tate.rb

It’s basically: get a stack of 2d arrays, chop them in half, rotate them  
around and plonk them on top. The key bit was probably figuring out that  
you could just rotate the entire piece of paper, fold one way and then  
rotate it back, so you don’t have to deal with the headaches of figuring  
out how each fold will mutate the arrays. I also added in an  
optimisation to keep track of the previous rotation so I didn’t have to  
burn cycles constantly rotating it back to the original orientation.  
Once I was done I sat down and wrote out each fold individually, but it  
ended up being slower than rotate and fold (I eventually optimised it to  
be slightly faster, but I preferred the simpler code of the rotate  
method).

Unfolding wasn’t very hard either, once you’d had a look at some stacks  
just prior to the last fold. If there was a difference of one between  
one of the rows on either side it was a left or right fold, otherwise it  
was a top or bottom fold (depending on which layer had the higher  
numbers). Did the same rotation magic to prevent headaches on figuring  
out the unfolding algorithyms.
