------art_21417_17312780.1137951744677
Content-Type: multipart/alternative; 
	boundary---art_21418_25426328.1137951744677"

------art_21418_25426328.1137951744677
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Attached is my solution--I wish very much that I knew about Array#transpose
when I wrote this.  Seems to work, though, although check_fold takes about 8
seconds on my zippy Pentium III 750MHz--some restructuring could improve
that.  From my observations, given a permutation of (1..256), there seems to
be at most one sequence of folds that gives that result.  i.e. there are
never two sequences that give the same permutation.  Does anybody know why
this is?  Seems like an interesting math problem.

I also really hacked together a get_dup method... how to you correctly tell
classes how to make copies of themselves?

------art_21418_25426328.1137951744677
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Attached is my solution--I wish very much that I knew about Array#transposehen I wrote this.  Seems to work, though, although check_fold takes about 8 seconds on my zippy Pentium III 750MHz--some restructuring could improve that.  From my observations, given a permutation of (1..256), there seems to be at most one sequence of folds that gives that result.  
i.e. there are never two sequences that give the same permutation.&nbsp; Does anybody know why this is?&nbsp; Seems like an interesting math problem.<br><br>I also really hacked together a get_dup method... how to you correctly tell classes how to make copies of themselves?
<br>

------art_21418_25426328.1137951744677--

------art_21417_17312780.1137951744677
Content-Type: image/rb; name=rubyquiz63.rb
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="rubyquiz63.rb"

class FoldedPaper
  attr_reader :xlength,:ylength,:thickness
  attr_accessor :sheets
  
  @@dict  'B' 0, 'R' 1, 'T' 2, 'L' 3}
  @@rev_dict  0 'B', 1 'R', 2 'T', 3 'L'}
  
  def initialize(x,y,thickness  )
    @xlength  
    @ylength  
    @thickness  hickness
    @sheets  rray.new(thickness)
    @sheets.each_index do |i|
      @sheets[i]  rray.new(y)
      @sheets[i].each_index {|j| @sheets[i][j]  rray.new(x)}
    end
  end
  
  def fill
    raise if @thickness ! 
    (0...@xlength).each {|a| (0...@ylength).each {|b| @sheets[0][b][a]  +b*@xlength+1}}
  end
  
  def peek
    @sheets.each_with_index do |sheet,i|
      puts "Sheet " + i.to_s + ":"
      sheet.each {|row| puts row.to_s}
      puts ""
    end
  end
  
  def rotate!
    new_sheets  rray.new(@sheets.length)
    new_sheets.each_index do |i|
      new_sheets[i]  rray.new(@xlength)
      (0...@xlength).each do |a|
        new_sheets[i][a]  rray.new(@ylength)
        (0...@ylength).each {|b| new_sheets[i][a][b]  sheets[i][@ylength-b-1][a]}
      end
    end
    @xlength,@ylength  ylength,@xlength
    @sheets  ew_sheets
  end
  
  def fold_bottom_to_top!
    raise if @ylength % 2 ! 
    new_sheets  rray.new(2*@thickness)
    (0...@thickness).each do |i|
      new_sheets[i]  rray.new(@ylength/2)
      new_sheets[i+@thickness]  rray.new(@ylength/2)
      (0...@ylength/2).each do |b|
        new_sheets[i][b]  rray.new(@xlength)
        new_sheets[i+@thickness][@ylength/2 - b-1]  rray.new(@xlength)
        (0...@xlength).each do |a|
          new_sheets[i][b][a]  sheets[i][b][a]
          new_sheets[i+@thickness][@ylength/2 - b-1][a]  sheets[@thickness-i-1][@ylength/2+b][a]
        end
      end
    end
    @ylength / 
    @thickness * 
    @sheets  ew_sheets
  end
  
  def fold!(k)o
    k.times {rotate!}
    fold_bottom_to_top!
    (4-k).times {rotate!}
  end
  
  def unfold_bottom_to_top!
    raise if @thickness % 2 ! 
    new_sheets  rray.new(@thickness/2)
    (0...@thickness/2).each do |i|
      new_sheets[i]  rray.new(2*@ylength)
      (0...@ylength).each do |b|
        new_sheets[i][b]  rray.new(@xlength)
        new_sheets[i][2*@ylength-b-1]  rray.new(@xlength)
        (0...@xlength).each do |a|
          new_sheets[i][b][a]  sheets[i][b][a]
          new_sheets[i][2*ylength-b-1][a]  sheets[@thickness-i-1][b][a]
        end
      end
    end
    @ylength * 
    @thickness / 
    @sheets  ew_sheets
  end
  
  def unfold!(k)
    k.times {rotate!}
    unfold_bottom_to_top!
    (4-k).times {rotate!}
  end
  
  def readdown
    raise if @xlength > 1 or @ylength > 1
    (0...@thickness).map {|i| @sheets[@thickness-i-1][0][0]}
  end
  
  def writedown array
    raise if @xlength > 1 or @ylength > 1 or @thickness ! rray.length
    array.each_with_index {|v,i| @sheets[@thickness-i-1][0][0]  }
  end
  
  def execute! command
    command.split("").each {|c| fold! @@dict[c]}
  end
  
  def make_dup_of! fp
    @sheets  p.sheets.dup
    @xlength  p.xlength
    @ylength  p.ylength
    @thickness  p.thickness
  end
  
  def get_dup
    fp  oldedPaper.new(1,1)
    fp.make_dup_of! self
    fp
  end
  
  def equals? fp
    return false if @xlength ! p.xlength or @ylength ! p.ylength or @thickness ! p.thickness
    (0...@thickness).each do |i|
      (0...@xlength).each do |a|
        (0...@ylength).each do |b|
          return false if @sheets[i][b][a] ! p.sheets[i][b][a]
        end
      end
    end
    return true
  end
  
  def is_valid? unfolded_x, unfolded_y
    if @thickness 1
      fp  oldedPaper.new(@xlength,@ylength)
      fp.fill
      return fp.equals?(self)
    end
  
    (0...@thickness).each do |i|
      (0...@xlength).each do |a|
        (0...@ylength).each do |b|
          value  sheets[i][b][a]
          neighbors  [a,b-1],[a,b+1],[a-1,b],[a+1,b]].select do |x,y|
            x >  and y >  and x < @xlength and y < @ylength
          end
          neighbor_values  eighbors.map {|x,y| @sheets[i][y][x]}
          if neighbor_values.detect {|n_val| not [1,unfolded_x].include?((value-n_val).abs)}
            return false
          end
        end
      end
    end
    return true
  end
  
  def find_solutions unfolded_x, unfolded_y
    solutions  ]
    if is_valid? unfolded_x, unfolded_y
      return [""] if @thickness 1
      (0..3).each do |k|
        fp  et_dup
        fp.unfold! k
        if fp.xlength < nfolded_x and fp.ylength < nfolded_y
          fp.find_solutions(unfolded_x,unfolded_y).each {|solution| solutions.push(solution + @@rev_dict[k])}
        end
      end
    end
    solutions
  end
end

def fold(x,y,s)
  fp  oldedPaper.new(x,y)
  fp.fill
  fp.execute! s
  fp.readdown
end

def check_fold x,y,array
  fp  oldedPaper.new(1,1,x*y)
  fp.writedown array
  fp.find_solutions(x,y)[0]
end
------art_21417_17312780.1137951744677--