From: Edward Faulkner Date: 2006-01-23T18:47:49+09:00 Subject: Re: [QUIZ][SOLUTION] Grid Folding (#63) --Yylu36WmvOXNoKYn Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable This is the kind of problem where it makes sense to trade off efficiency for simplicity. =20 class Paper def initialize(width, height) @grid =3D Array.new(width) { Array.new(height) } height.times { |y| width.times {|x| @grid[x][y] =3D [width*y+x+1] }} end def width; @grid.size; end def height; @grid[0].size; end def answer raise "Not enough folds" if width > 1 or height > 1 @grid[0][0] end # Fold right side over to left def fold! raise "Bad input" if width%2 =3D=3D 1 @grid =3D (0 .. (width / 2 - 1)).map { |col| @grid[col].zip(@grid[width - col - 1]).map {|pair| pair[1].reverse + = pair[0]} } end # 90 degree counter clockwise rotation def rotate! @grid =3D @grid.transpose.map{|c| c.reverse} end end def fold(width, height, commands) turns =3D commands.tr('RBLT', '0123').split(//).map{|x| x.to_i} paper =3D Paper.new(width, height) turns.each { |turn| 4.times {|i| paper.fold! if i=3D=3Dturn paper.rotate! } } =20 paper.answer end --Yylu36WmvOXNoKYn Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) iD8DBQFD1KYknhUz11p9MSARAo31AJ0ZVoQM7on1ogLTvRwU3yEjxVPWMgCeOGh8 GLZHoli43xG7bPWppnKCd2M= =uO2s -----END PGP SIGNATURE----- --Yylu36WmvOXNoKYn--