Forum: Ruby Re: Grid Folding (#63)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Daniel S. (Guest)
on 2006-01-24 07:29
(Received via mailing list)
My solution, performs all the extra credits.

http://members.iinet.net.au/~removed_email_address...
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.
This topic is locked and can not be replied to.